From a569be5c3b1eb634ad34125f03db3576e0cd10d0 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 10 Mar 2020 18:05:49 +0800 Subject: [PATCH] fix http bug --- CHANGELOG.md | 2 ++ .../main/java/cn/hutool/http/HttpRequest.java | 5 ++- .../java/cn/hutool/http/HttpResponse.java | 31 ++++++++++--------- .../cn/hutool/http/test/HttpRequestTest.java | 7 +++++ 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dce007f79..f0b7fa0b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,9 @@ ## 5.2.2 ### 新特性 + ### Bug修复 +* 【http 】 修复body方法添加多余头的问题(issue#769@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java index a4d2ddd4d..74b39ee33 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -675,7 +675,6 @@ public class HttpRequest extends HttpBase { byte[] bytes = StrUtil.bytes(body, this.charset); body(bytes); this.form = null; // 当使用body时,停止form的使用 - contentLength(bytes.length); if (null != contentType) { // Content-Type自定义设置 @@ -695,6 +694,7 @@ public class HttpRequest extends HttpBase { // 判断是否为rest请求 if (StrUtil.containsAnyIgnoreCase(contentType, "json", "xml")) { this.isRest = true; + contentLength(bytes.length); } return this; } @@ -929,6 +929,7 @@ public class HttpRequest extends HttpBase { if (this.encodeUrlParams) { this.url = HttpUtil.encodeParams(this.url, this.charset); } + // 初始化 connection initConnection(); @@ -942,6 +943,7 @@ public class HttpRequest extends HttpBase { if (null == httpResponse) { httpResponse = new HttpResponse(this.httpConnection, this.charset, isAsync, isIgnoreResponseBody()); } + return httpResponse; } @@ -1034,6 +1036,7 @@ public class HttpRequest extends HttpBase { this.httpConnection.disconnectQuietly(); throw new HttpException(e); } + if (responseCode != HttpURLConnection.HTTP_OK) { if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_SEE_OTHER) { this.url = httpConnection.header(Header.LOCATION); diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java b/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java index 9dd7210b4..f309b8669 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpResponse.java @@ -1,5 +1,17 @@ package cn.hutool.http; +import cn.hutool.core.convert.Convert; +import cn.hutool.core.io.FastByteArrayOutputStream; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.IORuntimeException; +import cn.hutool.core.io.IoUtil; +import cn.hutool.core.io.StreamProgress; +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.core.util.ReUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.core.util.URLUtil; +import cn.hutool.http.cookie.GlobalCookieManager; + import java.io.ByteArrayInputStream; import java.io.Closeable; import java.io.EOFException; @@ -13,18 +25,6 @@ import java.nio.charset.Charset; import java.util.List; import java.util.Map.Entry; -import cn.hutool.core.convert.Convert; -import cn.hutool.core.io.FastByteArrayOutputStream; -import cn.hutool.core.io.FileUtil; -import cn.hutool.core.io.IORuntimeException; -import cn.hutool.core.io.IoUtil; -import cn.hutool.core.io.StreamProgress; -import cn.hutool.core.util.CharsetUtil; -import cn.hutool.core.util.ReUtil; -import cn.hutool.core.util.StrUtil; -import cn.hutool.core.util.URLUtil; -import cn.hutool.http.cookie.GlobalCookieManager; - /** * Http响应类
* 非线程安全对象 @@ -111,7 +111,7 @@ public class HttpResponse extends HttpBase implements Closeable { */ public boolean isGzip() { final String contentEncoding = contentEncoding(); - return contentEncoding != null && "gzip".equalsIgnoreCase(contentEncoding); + return "gzip".equalsIgnoreCase(contentEncoding); } /** @@ -122,7 +122,7 @@ public class HttpResponse extends HttpBase implements Closeable { */ public boolean isDeflate() { final String contentEncoding = contentEncoding(); - return contentEncoding != null && "deflate".equalsIgnoreCase(contentEncoding); + return "deflate".equalsIgnoreCase(contentEncoding); } /** @@ -133,7 +133,7 @@ public class HttpResponse extends HttpBase implements Closeable { */ public boolean isChunked() { final String transferEncoding = header(Header.TRANSFER_ENCODING); - return transferEncoding != null && "Chunked".equalsIgnoreCase(transferEncoding); + return "Chunked".equalsIgnoreCase(transferEncoding); } /** @@ -379,6 +379,7 @@ public class HttpResponse extends HttpBase implements Closeable { // 服务器无返回内容,忽略之 } + // 读取响应头信息 try { this.headers = httpConnection.headers(); diff --git a/hutool-http/src/test/java/cn/hutool/http/test/HttpRequestTest.java b/hutool-http/src/test/java/cn/hutool/http/test/HttpRequestTest.java index 51b4c93b5..96836d5fb 100644 --- a/hutool-http/src/test/java/cn/hutool/http/test/HttpRequestTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/test/HttpRequestTest.java @@ -106,4 +106,11 @@ public class HttpRequestTest { .execute().body(); Console.log(res); } + + @Test + @Ignore + public void bodyTest(){ + String ddddd1 = HttpRequest.get("https://baijiahao.baidu.com/s").body("id=1625528941695652600").execute().body(); + Console.log(ddddd1); + } }