diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/Request.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/Request.java index 77497f661..4e67d83c0 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/Request.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/Request.java @@ -204,16 +204,6 @@ public class Request implements HeaderOperation { return MapUtil.view(this.headers); } - /** - * 是否为Transfer-Encoding:Chunked的内容 - * - * @return 是否为Transfer-Encoding:Chunked的内容 - */ - public boolean isChunked() { - final String transferEncoding = header(HeaderName.TRANSFER_ENCODING); - return "Chunked".equalsIgnoreCase(transferEncoding); - } - /** * 设置一个header
* 如果覆盖模式,则替换之前的值,否则加入到值列表中
diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java index 894466570..190388638 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java @@ -21,14 +21,13 @@ import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.http.HttpException; import org.dromara.hutool.http.HttpUtil; import org.dromara.hutool.http.client.ClientConfig; -import org.dromara.hutool.http.client.engine.ClientEngine; import org.dromara.hutool.http.client.Request; import org.dromara.hutool.http.client.Response; import org.dromara.hutool.http.client.body.HttpBody; import org.dromara.hutool.http.client.cookie.GlobalCookieManager; +import org.dromara.hutool.http.client.engine.ClientEngine; import org.dromara.hutool.http.meta.HeaderName; import org.dromara.hutool.http.meta.HttpStatus; -import org.dromara.hutool.http.meta.Method; import java.io.IOException; import java.net.HttpURLConnection; @@ -125,11 +124,21 @@ public class JdkClientEngine implements ClientEngine { .setSSLInfo(config.getSslInfo()) // 关闭JDK自动转发,采用手动转发方式 .setInstanceFollowRedirects(false) - .setChunkedStreamingMode(message.isChunked() ? 4096 : -1) .setDisableCache(config.isDisableCache()) // 覆盖默认Header .header(message.headers(), true); + if(!message.method().isIgnoreBody()){ + // 在允许发送body的情况下,如果用户自定义了Content-Length,则使用用户定义的值 + final long contentLength = message.contentLength(); + if(contentLength > 0){ + // 固定请求长度 + conn.setFixedLengthStreamingMode(contentLength); + } else if(message.isChunked()){ + conn.setChunkedStreamingMode(4096); + } + } + if (null == message.header(HeaderName.COOKIE)) { // 用户没有自定义Cookie,则读取全局Cookie信息并附带到请求中 GlobalCookieManager.add(conn); @@ -169,8 +178,7 @@ public class JdkClientEngine implements ClientEngine { } // 最终页面 - return new JdkHttpResponse(conn, true, message.charset(), isAsync, - isIgnoreResponseBody(message.method())); + return new JdkHttpResponse(conn, true, message.charset(), isAsync, message.method().isIgnoreBody()); } /** @@ -208,18 +216,4 @@ public class JdkClientEngine implements ClientEngine { return redirectUrl; } - - /** - * 是否忽略读取响应body部分
- * HEAD、CONNECT、TRACE方法将不读取响应体 - * - * @return 是否需要忽略响应body部分 - */ - private boolean isIgnoreResponseBody(final Method method) { - //https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Methods/OPTIONS - // OPTIONS请求可以带有响应体 - return Method.HEAD == method - || Method.CONNECT == method - || Method.TRACE == method; - } } diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/meta/Method.java b/hutool-http/src/main/java/org/dromara/hutool/http/meta/Method.java index a17c13f53..90df6d5f3 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/meta/Method.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/meta/Method.java @@ -18,5 +18,24 @@ package org.dromara.hutool.http.meta; * @author Looly */ public enum Method { - GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, CONNECT, PATCH + GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, CONNECT, PATCH; + + /** + * 是否忽略读取响应body部分
+ * HEAD、CONNECT、TRACE方法将不读取响应体 + * + * @return 是否需要忽略响应body部分 + */ + public boolean isIgnoreBody() { + //https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Methods/OPTIONS + // OPTIONS请求可以带有响应体 + switch (this){ + case HEAD: + case CONNECT: + case TRACE: + return true; + default: + return false; + } + } } diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/HttpUtilTest.java b/hutool-http/src/test/java/org/dromara/hutool/http/HttpUtilTest.java index 280428906..9dcee1071 100644 --- a/hutool-http/src/test/java/org/dromara/hutool/http/HttpUtilTest.java +++ b/hutool-http/src/test/java/org/dromara/hutool/http/HttpUtilTest.java @@ -27,7 +27,6 @@ import org.junit.jupiter.api.Test; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.TimeUnit; @SuppressWarnings("resource") public class HttpUtilTest { @@ -97,6 +96,8 @@ public class HttpUtilTest { @Test @Disabled public void get12306Test() { + // 某些网站需要打开信任全部域 + // HttpGlobalConfig.setTrustAnyHost(true); HttpUtil.send(Request.of("https://kyfw.12306.cn/otn/").setMaxRedirectCount(2)) .then(response -> Console.log(response.bodyStr())); } @@ -195,7 +196,7 @@ public class HttpUtilTest { @Test @Disabled public void httpUtilCreateRequest1PostSoap11Test(){ - String requestBody = "\n" + + final String requestBody = "\n" + "\n" + " \n" + @@ -205,7 +206,7 @@ public class HttpUtilTest { " \n" + ""; - String body = HttpUtil.createRequest("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx", Method.POST) + final String body = HttpUtil.createRequest("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx", Method.POST) .header(HeaderName.CONTENT_TYPE, "text/xml; charset=utf-8") .header("Accept", "application/xml") .header("accessId", "") @@ -222,7 +223,7 @@ public class HttpUtilTest { @Test @Disabled public void httpUtilCreateRequest2PostSoap12Test(){ - String requestBody = "\n" + + final String requestBody = "\n" + "\n" + " \n" + @@ -232,7 +233,7 @@ public class HttpUtilTest { " \n" + ""; - String body = HttpUtil.createPost("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx") + final String body = HttpUtil.createPost("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx") .header(HeaderName.CONTENT_TYPE, "application/soap+xml; charset=utf-8") .header("Accept", "application/xml") .header("accessId", "") @@ -255,7 +256,7 @@ public class HttpUtilTest { ; //设置超时 HttpGlobalConfig.setTimeout(1); - String body = HttpUtil.createGet("https://echo.apifox.com/get") + final String body = HttpUtil.createGet("https://echo.apifox.com/get") .header(HeaderName.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=utf-8") .header("User-Agent", "Apifox/1.0.0 (https://apifox.com)") .header("Accept", "*/*") @@ -272,8 +273,8 @@ public class HttpUtilTest { @Test @Disabled public void httpUtilCreateRequest4PostTest(){ - String requestBodyJson = "{\n\"username\": \"张三\",\n \"password\": \"abcdefg@123\"\n}"; - String body = HttpUtil.createPost("https://echo.apifox.com/post?q1=v1&q2=v2") + final String requestBodyJson = "{\n\"username\": \"张三\",\n \"password\": \"abcdefg@123\"\n}"; + final String body = HttpUtil.createPost("https://echo.apifox.com/post?q1=v1&q2=v2") .header("User-Agent", "Apifox/1.0.0 (https://apifox.com)") .header("Content-Type", "application/json") .header("Accept", "*/*") diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/client/JdkEngineTest.java b/hutool-http/src/test/java/org/dromara/hutool/http/client/JdkClientEngineTest.java similarity index 73% rename from hutool-http/src/test/java/org/dromara/hutool/http/client/JdkEngineTest.java rename to hutool-http/src/test/java/org/dromara/hutool/http/client/JdkClientEngineTest.java index f90c9ce7e..17d819142 100644 --- a/hutool-http/src/test/java/org/dromara/hutool/http/client/JdkEngineTest.java +++ b/hutool-http/src/test/java/org/dromara/hutool/http/client/JdkClientEngineTest.java @@ -19,7 +19,7 @@ import org.dromara.hutool.http.meta.Method; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -public class JdkEngineTest { +public class JdkClientEngineTest { @Test @Disabled @@ -34,4 +34,20 @@ public class JdkEngineTest { Console.log(res.headers()); Console.log(res.bodyStr()); } + + @Test + @Disabled + public void postTest(){ + final ClientEngine engine = HttpUtil.createClient("jdkClient"); + + final Request req = Request.of("https://www.hutool.cn/") + .method(Method.POST) + .body("a=1&b=2"); + final Response res = engine.send(req); + + Console.log(res.getStatus()); + Console.log(res.headers().getClass()); + Console.log(res.headers()); + Console.log(res.bodyStr()); + } }