一、依赖引入
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.7</version> </dependency>
二、使用HttpClient跳过证书验证,主要是在构建HttpClient对象时。需要添加SSLContext与SSLConnectionSocketFactory配置.核心代码如下:
private CloseableHttpClient createHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { SSLContext sslcontext = SSLContexts.custom() .loadTrustMaterial(null, (chain, authType) -> true) .build(); SSLConnectionSocketFactory sslSf = new SSLConnectionSocketFactory(sslcontext, null, null, new NoopHostnameVerifier()); return HttpClients.custom().setSSLSocketFactory(sslSf).build(); }