本文介绍了PoolingHttpClientConnectionManager:如何做Https请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用 CloseableHttpClient 同时执行多个 HttpGet 请求。

我用Google搜索了如何做到这一点,答案是使用 PoolingHttpClientConnectionManager

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.

此时我得到了这个:

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

然后我尝试了 HttpGet 请求 http://www.google.com ,一切正常。

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

然后我创建了一个信任库cmd并导入目标网站的证书,使用我的信任库设置 SSLConnectionSocketFactory 并设置 SSLSocketFactory httpClient

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();

如果我尝试执行Https HttpGet 然后我得到一个 PKIX路径构建失败异常。

如果我这样做而没有 .setConnectionManager(cManager)一切正常。

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.

你们有谁能告诉我如何才能让它发挥作用? (别担心,我不创建任何ddos工具)

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

提前致谢!

PS :我正在使用HttpComponents 4.3.1

P.S.: I'm using HttpComponents 4.3.1

推荐答案

找到答案:

只需添加

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

并传递 socketFactoryRegistry 作为构造函数的参数 PoolingHttpClientConnectionManager
现在它的工作正常:)

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

这篇关于PoolingHttpClientConnectionManager:如何做Https请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 19:46