hutool工具HttpRequest绕过ssl
时间: 2023-09-29 15:06:38 浏览: 653
Hutool工具中的HttpRequest默认会进行SSL验证,如果需要绕过SSL验证,可以通过以下两种方式实现:
1. 直接忽略SSL证书
```java
HttpRequest.get("https://www.example.com")
.setSSLProtocol("TLSv1.2")
.setIgnoreSSL(true)
.execute();
```
2. 设置自定义的SSL上下文
```java
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, (chain, authType) -> true)
.build();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", sslSocketFactory)
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
HttpClient client = HttpClients.custom()
.setConnectionManager(connectionManager)
.build();
Request.get("https://www.example.com")
.setHttpClient(client)
.execute();
```
以上两种方法都可以实现绕过SSL验证的功能,但第二种方法更为安全,推荐使用。
阅读全文