mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -204,16 +204,6 @@ public class Request implements HeaderOperation<Request> {
|
||||
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<br>
|
||||
* 如果覆盖模式,则替换之前的值,否则加入到值列表中<br>
|
||||
|
@@ -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部分<br>
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
@@ -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部分<br>
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user