[httpclient学习]-httpclient 4.3 https信任所有证书

说明:

    httpclient 每个版本变化都很大,所以不敢保证适用于所有httpclient版本。当前版本httpclient4.3。

    代码:

//自定义的socket工厂类可以和指定的协议(Http、Https)联系起来,用来创建自定义的连接管理器。
RegistryBuilder<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory> create();
PlainConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
//https信任所有证书
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
    @Override
    public boolean isTrusted(
	java.security.cert.X509Certificate[] chain,
	String authType) throws CertificateException {
	    // TODO Auto-generated method stub
	    return true;
	}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
r = r.register("https", sslsf);
r = r.register("http", plainsf);
//连接池管理器
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(r.build());
connectionManager.setMaxTotal(100);
//cookie
CookieStore cookieStore = new BasicCookieStore();

HttpClientBuilder httpClientBuilder = HttpClients.custom()
    .setConnectionManager(connectionManager)
    .setDefaultCookieStore(cookieStore);
    
RequestConfig globalconfig = RequestConfig.custom()
    .setRedirectsEnabled(true)
    .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
    
CloseableHttpClient httpClient = httpClientBuilder.setDefaultRequestConfig(globalconfig).build();
    

 

                


你可能感兴趣的:(https,证书,HttpClient4.3)