This commit is contained in:
Looly 2023-09-08 19:09:40 +08:00
parent 57bb186f24
commit 83e94584bf
5 changed files with 48 additions and 5 deletions

View File

@ -143,6 +143,15 @@ public class ClientConfig {
return this; return this;
} }
/**
* 打开SSL验证即使用引擎默认的SSL验证方式
*
* @return this
*/
public ClientConfig enableSSLVerify(){
return setSSLInfo(SSLInfo.DEFAULT);
}
/** /**
* 是否禁用缓存 * 是否禁用缓存
* *

View File

@ -22,10 +22,11 @@ import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.http.GlobalHeaders; import org.dromara.hutool.http.GlobalHeaders;
import org.dromara.hutool.http.HttpGlobalConfig; import org.dromara.hutool.http.HttpGlobalConfig;
import org.dromara.hutool.http.HttpUtil;
import org.dromara.hutool.http.client.body.HttpBody; import org.dromara.hutool.http.client.body.HttpBody;
import org.dromara.hutool.http.client.body.StringBody; import org.dromara.hutool.http.client.body.StringBody;
import org.dromara.hutool.http.client.body.UrlEncodedFormBody; import org.dromara.hutool.http.client.body.UrlEncodedFormBody;
import org.dromara.hutool.http.client.engine.ClientEngine;
import org.dromara.hutool.http.client.engine.ClientEngineFactory;
import org.dromara.hutool.http.meta.HeaderName; import org.dromara.hutool.http.meta.HeaderName;
import org.dromara.hutool.http.meta.Method; import org.dromara.hutool.http.meta.Method;
@ -296,6 +297,16 @@ public class Request implements HeaderOperation<Request> {
* @return 响应内容 * @return 响应内容
*/ */
public Response send() { public Response send() {
return HttpUtil.send(this); return send(ClientEngineFactory.getEngine());
}
/**
* 发送请求
*
* @param engine 自自定义引擎
* @return 响应内容
*/
public Response send(final ClientEngine engine){
return engine.send(this);
} }
} }

View File

@ -12,10 +12,10 @@
package org.dromara.hutool.http.client.engine.jdk; package org.dromara.hutool.http.client.engine.jdk;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.net.url.URLUtil; import org.dromara.hutool.core.net.url.URLUtil;
import org.dromara.hutool.core.reflect.FieldUtil; import org.dromara.hutool.core.reflect.FieldUtil;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.http.HttpException; import org.dromara.hutool.http.HttpException;
import org.dromara.hutool.http.client.HeaderOperation; import org.dromara.hutool.http.client.HeaderOperation;
import org.dromara.hutool.http.meta.Method; import org.dromara.hutool.http.meta.Method;
@ -221,8 +221,8 @@ public class JdkHttpConnection implements HeaderOperation<JdkHttpConnection> {
// Https请求 // Https请求
final HttpsURLConnection httpsConn = (HttpsURLConnection) conn; final HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
// 验证域 // 验证域
httpsConn.setHostnameVerifier(ObjUtil.defaultIfNull(sslInfo.getHostnameVerifier(), SSLInfo.TRUST_ANY.getHostnameVerifier())); Opt.ofNullable(sslInfo.getHostnameVerifier()).ifPresent(httpsConn::setHostnameVerifier);
httpsConn.setSSLSocketFactory(ObjUtil.defaultIfNull(sslInfo.getSocketFactory(), SSLInfo.TRUST_ANY.getSocketFactory())); Opt.ofNullable(sslInfo.getSocketFactory()).ifPresent(httpsConn::setSSLSocketFactory);
} }
return this; return this;

View File

@ -176,6 +176,9 @@ public class SSLInfo {
* @return {@link SSLSocketFactory} * @return {@link SSLSocketFactory}
*/ */
public SSLSocketFactory getSocketFactory() { public SSLSocketFactory getSocketFactory() {
if(null == this.sslContext){
return null;
}
final SSLSocketFactory factory = this.sslContext.getSocketFactory(); final SSLSocketFactory factory = this.sslContext.getSocketFactory();
return new CustomProtocolsSSLFactory(factory, this.protocols); return new CustomProtocolsSSLFactory(factory, this.protocols);
} }

View File

@ -0,0 +1,20 @@
package org.dromara.hutool.http.client;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.http.client.engine.jdk.JdkClientEngine;
import org.dromara.hutool.http.ssl.SSLInfo;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
public class IssueI7ZRJUTest {
@SuppressWarnings({"resource", "TestFailedLine"})
@Test
@Disabled
void getBadSSlTest() {
final Response response = Request.of("https://expired.badssl.com/")
.send(new JdkClientEngine().init(ClientConfig.of().setSSLInfo(SSLInfo.DEFAULT)));
Console.log(response.body());
}
}