简体   繁体   English

如何忽略 Apache HttpComponents HttpClient 5.1 中的 SSL 证书错误

[英]How to ignore SSL certificate errors in Apache HttpComponents HttpClient 5.1

How do I bypass certificate verification errors with Apache HttpComponents HttpClient 5.1 ?如何使用Apache HttpComponents HttpClient 5.1绕过证书验证错误?

I've found a working solution to bypass such errors in HttpClient 4.5 that suggests customizing HttpClient instance:我找到了一个可行的解决方案来绕过 HttpClient 4.5 中的此类错误,建议自定义HttpClient实例:

HttpClient httpClient = HttpClients
            .custom()
            .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build())
            .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
            .build();

But it is not applicable to HttpClient 5.1, as setSSLContext and setSSLHostnameVerifier methods do not exist in HttpClientBuilder (which HttpClients.custom() returns).但它不适用于 HttpClient 5.1,因为setSSLContextsetSSLHostnameVerifier方法在HttpClientBuilderHttpClients.custom()返回)中不存在。

There are several specialized builders in HC 5.1 that can be used to do the same: HC 5.1 中有几个专门的构建器可用于执行相同的操作:

CloseableHttpClient httpclient = HttpClients.custom()
        .setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
                .setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create()
                        .setSslContext(SSLContextBuilder.create()
                                .loadTrustMaterial(TrustAllStrategy.INSTANCE)
                                .build())
                        .setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                        .build())
                .build())
        .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM