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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
|
||||
final String requestBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
|
||||
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
|
||||
" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
|
||||
" <soap:Body>\n" +
|
||||
@@ -205,7 +206,7 @@ public class HttpUtilTest {
|
||||
" </soap:Body>\n" +
|
||||
"</soap:Envelope>";
|
||||
|
||||
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 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
|
||||
final String requestBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
|
||||
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
|
||||
" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
|
||||
" <soap12:Body>\n" +
|
||||
@@ -232,7 +233,7 @@ public class HttpUtilTest {
|
||||
" </soap12:Body>\n" +
|
||||
"</soap12:Envelope>";
|
||||
|
||||
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", "*/*")
|
||||
|
@@ -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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user