16

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

TorbenJ
  • 4,462
  • 11
  • 47
  • 84
  • ` CloseableHttpClient httpClient = HttpClients.custom() .setSSLSocketFactory(sslsf) .setConnectionManager(cManager) .build(); ` Setting SSLSocketFacotry() here is not useful here as the socket factory set inside the connectionmanager via registry is used. So when when you set connection manager, no need to set sslsocketfactory() while building client – Dheeraj Kumar Gopali Jan 12 '21 at 16:42

1 Answers1

36

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 :)

Community
  • 1
  • 1
TorbenJ
  • 4,462
  • 11
  • 47
  • 84
  • How to use PoolingHttpClientConnectionManager after creating this , my code is working but i want to know does connection pooling works or not – Labeo Sep 24 '15 at 06:31
  • 1
    turn on client logging to check leases from the connection pool: http://hc.apache.org/httpcomponents-client-ga/logging.html – Jake B Jul 09 '16 at 20:51
  • great, I spent 2 days on this, and with your help it got resolved in 5 minutes. I wish if I could I have seen this earlier.. Thank You. – Rahul Singh May 20 '18 at 19:26
  • If you want 'http' as well as 'https', check this comment - https://stackoverflow.com/a/20491564/3010484 – hipokito Mar 19 '20 at 14:54