Java PoolingHttpClientConnectionManager:如何做 Https 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20511020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PoolingHttpClientConnectionManager: How to do Https requests?
提问by TorbenJ
I'm currently trying to do multiple HttpGetrequests at the same time with CloseableHttpClient.
I googled on how to do that and the answer was to use a PoolingHttpClientConnectionManager.
我目前正在尝试HttpGet使用CloseableHttpClient.
我用谷歌搜索了如何做到这一点,答案是使用PoolingHttpClientConnectionManager.
At this point I got this:
此时我得到了这个:
PoolingHttpClientConnectionManager cManager = new PoolingHttpClientConnectionManager();
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cManager)
.build();
Then I tried a HttpGetrequest to http://www.google.comand everything worked fine.
然后我尝试了一个HttpGet请求http://www.google.com,一切正常。
Then I created a truststore via cmd and imported the certificate of the targeted website, setup a SSLConnectionSocketFactorywith my truststore and set the SSLSocketFactoryof httpClient:
然后,我创建了一个信任通过CMD并导入目标网站的证书,设置一个SSLConnectionSocketFactory与我的信任和设置SSLSocketFactory的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 HttpGetthen I get a PKIX path building failedexception.
If I do the same without .setConnectionManager(cManager)everything works fine.
如果我尝试执行 Https,HttpGet则会出现PKIX path building failed异常。
如果我在没有.setConnectionManager(cManager)一切的情况下做同样的工作。
Can anyone of you tell me how I can get this to work? (Don't worry, I don't create any ddos tool)
你们中的任何人都可以告诉我如何让这个工作?(别担心,我不创建任何 ddos 工具)
Thanks in advance!
提前致谢!
P.S.: I'm using HttpComponents 4.3.1
PS:我使用的是 HttpComponents 4.3.1
采纳答案by TorbenJ
Found the answer: https://stackoverflow.com/a/19950935/1223253
找到答案:https: //stackoverflow.com/a/19950935/1223253
Just had to add
只好补充
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create().register("https", sslsf).build();
and pass socketFactoryRegistryas parameter to the constructor of PoolingHttpClientConnectionManager.
Now it works just fine :)
并socketFactoryRegistry作为参数传递给 的构造函数PoolingHttpClientConnectionManager。现在它工作得很好:)

