fix http bug

This commit is contained in:
Looly 2020-03-10 18:05:49 +08:00
parent ae5f8c0efe
commit a569be5c3b
4 changed files with 29 additions and 16 deletions

View File

@ -6,7 +6,9 @@
## 5.2.2 ## 5.2.2
### 新特性 ### 新特性
### Bug修复 ### Bug修复
* 【http 】 修复body方法添加多余头的问题issue#769@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -675,7 +675,6 @@ public class HttpRequest extends HttpBase<HttpRequest> {
byte[] bytes = StrUtil.bytes(body, this.charset); byte[] bytes = StrUtil.bytes(body, this.charset);
body(bytes); body(bytes);
this.form = null; // 当使用body时停止form的使用 this.form = null; // 当使用body时停止form的使用
contentLength(bytes.length);
if (null != contentType) { if (null != contentType) {
// Content-Type自定义设置 // Content-Type自定义设置
@ -695,6 +694,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
// 判断是否为rest请求 // 判断是否为rest请求
if (StrUtil.containsAnyIgnoreCase(contentType, "json", "xml")) { if (StrUtil.containsAnyIgnoreCase(contentType, "json", "xml")) {
this.isRest = true; this.isRest = true;
contentLength(bytes.length);
} }
return this; return this;
} }
@ -929,6 +929,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
if (this.encodeUrlParams) { if (this.encodeUrlParams) {
this.url = HttpUtil.encodeParams(this.url, this.charset); this.url = HttpUtil.encodeParams(this.url, this.charset);
} }
// 初始化 connection // 初始化 connection
initConnection(); initConnection();
@ -942,6 +943,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
if (null == httpResponse) { if (null == httpResponse) {
httpResponse = new HttpResponse(this.httpConnection, this.charset, isAsync, isIgnoreResponseBody()); httpResponse = new HttpResponse(this.httpConnection, this.charset, isAsync, isIgnoreResponseBody());
} }
return httpResponse; return httpResponse;
} }
@ -1034,6 +1036,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
this.httpConnection.disconnectQuietly(); this.httpConnection.disconnectQuietly();
throw new HttpException(e); throw new HttpException(e);
} }
if (responseCode != HttpURLConnection.HTTP_OK) { if (responseCode != HttpURLConnection.HTTP_OK) {
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_SEE_OTHER) { if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
this.url = httpConnection.header(Header.LOCATION); this.url = httpConnection.header(Header.LOCATION);

View File

@ -1,5 +1,17 @@
package cn.hutool.http; 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.ByteArrayInputStream;
import java.io.Closeable; import java.io.Closeable;
import java.io.EOFException; import java.io.EOFException;
@ -13,18 +25,6 @@ import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import java.util.Map.Entry; 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响应类<br> * Http响应类<br>
* 非线程安全对象 * 非线程安全对象
@ -111,7 +111,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
*/ */
public boolean isGzip() { public boolean isGzip() {
final String contentEncoding = contentEncoding(); final String contentEncoding = contentEncoding();
return contentEncoding != null && "gzip".equalsIgnoreCase(contentEncoding); return "gzip".equalsIgnoreCase(contentEncoding);
} }
/** /**
@ -122,7 +122,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
*/ */
public boolean isDeflate() { public boolean isDeflate() {
final String contentEncoding = contentEncoding(); final String contentEncoding = contentEncoding();
return contentEncoding != null && "deflate".equalsIgnoreCase(contentEncoding); return "deflate".equalsIgnoreCase(contentEncoding);
} }
/** /**
@ -133,7 +133,7 @@ public class HttpResponse extends HttpBase<HttpResponse> implements Closeable {
*/ */
public boolean isChunked() { public boolean isChunked() {
final String transferEncoding = header(Header.TRANSFER_ENCODING); 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<HttpResponse> implements Closeable {
// 服务器无返回内容忽略之 // 服务器无返回内容忽略之
} }
// 读取响应头信息 // 读取响应头信息
try { try {
this.headers = httpConnection.headers(); this.headers = httpConnection.headers();

View File

@ -106,4 +106,11 @@ public class HttpRequestTest {
.execute().body(); .execute().body();
Console.log(res); 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);
}
} }