Search in sources :

Example 1 with HttpResponse

use of cn.hutool.http.HttpResponse in project hutool by looly.

the class HttpRequestTest method asyncHeadTest.

@Test
@Ignore
public void asyncHeadTest() {
    HttpResponse response = HttpRequest.head(url).execute();
    Map<String, List<String>> headers = response.headers();
    Console.log(headers);
    Console.log(response.body());
}
Also used : HttpResponse(cn.hutool.http.HttpResponse) List(java.util.List) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with HttpResponse

use of cn.hutool.http.HttpResponse in project hutool by looly.

the class HttpRequestTest method uploadTest.

@Test
@Ignore
public void uploadTest() {
    File file = FileUtil.file("D:\\face.jpg");
    // 方法一:自定义构建表单
    HttpRequest request = // 
    HttpRequest.post(// 
    "http://localhost:8080/file/upload").form("file", // 
    file).form("fileType", "图片");
    HttpResponse response = request.execute();
    System.out.println(response.body());
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) HttpResponse(cn.hutool.http.HttpResponse) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with HttpResponse

use of cn.hutool.http.HttpResponse in project hutool by looly.

the class HttpRequestTest method asyncGetTest.

@Test
@Ignore
public void asyncGetTest() {
    TimeInterval timer = DateUtil.timer();
    HttpResponse body = HttpRequest.get(url).charset("GBK").executeAsync();
    long interval = timer.interval();
    timer.restart();
    Console.log(body.body());
    long interval2 = timer.interval();
    Console.log("Async response spend {}ms, body spend {}ms", interval, interval2);
}
Also used : TimeInterval(cn.hutool.core.date.TimeInterval) HttpResponse(cn.hutool.http.HttpResponse) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with HttpResponse

use of cn.hutool.http.HttpResponse in project hutool by looly.

the class SoapClient method sendForMessage.

/**
 * 执行Webservice请求,即发送SOAP内容
 *
 * @return 返回结果
 */
public SOAPMessage sendForMessage() {
    final HttpResponse res = sendForResponse();
    final MimeHeaders headers = new MimeHeaders();
    for (Entry<String, List<String>> entry : res.headers().entrySet()) {
        if (StrUtil.isNotEmpty(entry.getKey())) {
            headers.setHeader(entry.getKey(), CollUtil.get(entry.getValue(), 0));
        }
    }
    try {
        return this.factory.createMessage(headers, res.bodyStream());
    } catch (IOException | SOAPException e) {
        throw new SoapRuntimeException(e);
    } finally {
        IoUtil.close(res);
    }
}
Also used : MimeHeaders(javax.xml.soap.MimeHeaders) SOAPException(javax.xml.soap.SOAPException) HttpResponse(cn.hutool.http.HttpResponse) List(java.util.List) IOException(java.io.IOException)

Example 5 with HttpResponse

use of cn.hutool.http.HttpResponse in project Jpom by dromara.

the class TomcatManageService method tomcatCmd.

/**
 * 访问tomcat Url
 *
 * @param tomcatInfoModel tomcat信息
 * @param cmd             命令
 * @return 访问结果
 */
private String tomcatCmd(TomcatInfoModel tomcatInfoModel, String cmd) {
    String url = String.format("http://127.0.0.1:%d/jpomAgent/%s", tomcatInfoModel.getPort(), cmd);
    HttpRequest httpRequest = new HttpRequest(url);
    // 设置超时时间为3秒
    httpRequest.setConnectionTimeout(3000);
    String body = "";
    try {
        HttpResponse httpResponse = httpRequest.execute();
        if (httpResponse.isOk()) {
            body = httpResponse.body();
        }
        if (httpResponse.getStatus() == HttpStatus.HTTP_NOT_FOUND) {
            // 没有插件
            tomcatInfoModel.initTomcat();
            throw new JpomRuntimeException("tomcat 未初始化,已经重新初始化请稍后再试");
        }
    } catch (JpomRuntimeException jpom) {
        throw jpom;
    } catch (Exception ignored) {
    }
    return body;
}
Also used : HttpRequest(cn.hutool.http.HttpRequest) JpomRuntimeException(io.jpom.system.JpomRuntimeException) HttpResponse(cn.hutool.http.HttpResponse) JpomRuntimeException(io.jpom.system.JpomRuntimeException)

Aggregations

HttpResponse (cn.hutool.http.HttpResponse)15 HttpRequest (cn.hutool.http.HttpRequest)6 List (java.util.List)6 JpomRuntimeException (io.jpom.system.JpomRuntimeException)5 File (java.io.File)4 Ignore (org.junit.Ignore)4 Test (org.junit.Test)4 Convert (cn.hutool.core.convert.Convert)3 FileUtil (cn.hutool.core.io.FileUtil)3 Tuple (cn.hutool.core.lang.Tuple)3 Validator (cn.hutool.core.lang.Validator)3 StrUtil (cn.hutool.core.util.StrUtil)3 URLUtil (cn.hutool.core.util.URLUtil)3 Entity (cn.hutool.db.Entity)3 Page (cn.hutool.db.Page)3 ServletUtil (cn.hutool.extra.servlet.ServletUtil)3 HttpUtil (cn.hutool.http.HttpUtil)3 DefaultSystemLog (cn.jiangzeyin.common.DefaultSystemLog)3 JsonMessage (cn.jiangzeyin.common.JsonMessage)3 ValidatorItem (cn.jiangzeyin.common.validator.ValidatorItem)3