[ 登录 ] - [ 注册 ] | 开发者(推广)分成 无忧代理IP最新上线 | 代码示例 | IP测试视频 |

httpclient 4.5 https请求跳过证书验证

作者:数据无忧   时间:2019-02-13 14:50:15
再使用代理IP抓取HTTPS网站的数据过程中,报错:javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

解决方案:信任所有证书,并通过SslUtil.SslHttpClientBuild()方法获取CloseableHttpClient对象:

public class SslUtil {
public static CloseableHttpClient SslHttpClientBuild() {
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", trustAllHttpsCertificates()).build();
//创建ConnectionManager,添加Connection配置信息
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
return httpClient;
}
private static SSLConnectionSocketFactory trustAllHttpsCertificates() {
SSLConnectionSocketFactory socketFactory = null;
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager tm = new miTM();
trustAllCerts[0] = tm;
SSLContext sc = null;
try {
sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, null);
socketFactory = new SSLConnectionSocketFactory(sc, NoopHostnameVerifier.INSTANCE);
// HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return socketFactory;
}
static class miTM implements TrustManager, X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
//don't check
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
//don't check
}
}
}


无忧代理IP(www.data5u.com)原创文章,转载请注明出处。

抽取IP代金券
返回顶部
在线咨询
人工客服
 QQ交流群 - 001
QQ:340529947(满)
 QQ交流群 - 002
QQ:828393818
 售前咨询 - 001
QQ:1598729786
 技术支持 - 001
QQ:2562178315
为了避免 QQ 丢消息,请尽量添加客服为 QQ 好友。
客服工作时间08:30──17:30
关注微信
在线咨询
人工客服
 QQ交流群 - 001
QQ:340529947(满)
 QQ交流群 - 002
QQ:828393818
 售前咨询 - 001
QQ:1598729786
 技术支持 - 001
QQ:2562178315
为了避免 QQ 丢消息,请尽量添加客服为 QQ 好友。
客服工作时间08:30──17:30
电话:4007-745-096
QQ:
周一至周日8:30-18:00 技术部电话热线