PoolingHttpClientConnectionManager: How to do Https requests?

余生长醉 提交于 2019-11-28 17:57:59

问题


I'm currently trying to do multiple HttpGet requests at the same time with CloseableHttpClient.
I googled on how to do that and the answer was to use a PoolingHttpClientConnectionManager.

At this point I got this:

PoolingHttpClientConnectionManager cManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient httpClient = HttpClients.custom()
    .setConnectionManager(cManager)
    .build();

Then I tried a HttpGet request to http://www.google.com and everything worked fine.

Then I created a truststore via cmd and imported the certificate of the targeted website, setup a SSLConnectionSocketFactory with my truststore and set the SSLSocketFactory of httpClient:

KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream inputStream = new FileInputStream(new File("myTrustStore.truststore"));
trustStore.load(inputStream, "nopassword".toCharArray());
inputStream.close();

SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(trustStore).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

PoolingHttpClientConnectionManager cManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient httpClient = HttpClients.custom()
    .setSSLSocketFactory(sslsf)
    .setConnectionManager(cManager)
    .build();

If I try to execute a Https HttpGet then I get a PKIX path building failed exception.
If I do the same without .setConnectionManager(cManager) everything works fine.

Can anyone of you tell me how I can get this to work? (Don't worry, I don't create any ddos tool)

Thanks in advance!

P.S.: I'm using HttpComponents 4.3.1


回答1:


Found the answer: https://stackoverflow.com/a/19950935/1223253

Just had to add

Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create().register("https", sslsf).build();

and pass socketFactoryRegistry as parameter to the constructor of PoolingHttpClientConnectionManager. Now it works just fine :)



来源:https://stackoverflow.com/questions/20511020/poolinghttpclientconnectionmanager-how-to-do-https-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!