HttpClient4.3 关于https 中SSL证书请求问题,直接上代码 这里我使用的CloseableHttpClient这个实现类
调用此方法创建CloseableHttpClient 即可信任所有https SSL证书
SSLConnectionSocketFactory 中设置允许所有主机名称就可以忽略主机名称验证
private static CloseableHttpClient buildSSLCloseableHttpClient() throws Exception {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
//ALLOW_ALL_HOSTNAME_VERIFIER:这个主机名验证器基本上是关闭主机名验证的,实现的是一个空操作,并且不会抛出javax.net.ssl.SSLException异常。
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}
HttpClient4 用法 由HttpClient3 升级到 HttpClient4 必看
Android 实现 HttpClient 请求Https