简体   繁体   English

HttpClient 4.3.x,修复不推荐使用的代码以使用当前的 HttpClient 实现

[英]HttpClient 4.3.x, fixing deprecated code to use current HttpClient implementations

I had the following code, which still compiles, but they're all deprecated:我有以下代码,它仍然可以编译,但它们都已弃用:

SSLSocketFactory sslSocketFactory = new SSLSocketFactory(context, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager clientConnectionManager = base.getConnectionManager();
SchemeRegistry schemeRegistry = clientConnectionManager.getSchemeRegistry();
schemeRegistry.register(new Scheme("https", 443, sslSocketFactory));
return new DefaultHttpClient(clientConnectionManager, base.getParams());

I tried my best to replace it with this portion of the code:我尽力用这部分代码替换它:

HttpClientBuilder builder = HttpClientBuilder.create();
SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(context, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
builder.setConnectionManager(new BasicHttpClientConnectionManager());
builder.setSSLSocketFactory(sslConnectionFactory);
return builder.build();

As you can see, there are few lines of code from the top post that I don't know how to include on the new portion.如您所见,顶帖中的几行代码我不知道如何包含在新部分中。 How can I add needed code, such as, an alternate SchemeRegistry ?如何添加所需的代码,例如备用SchemeRegistry

我还不能发表评论,但这里是 herau 答案的一个小升级,因为它自 4.4 以来已被弃用,也许有人会发现它很有用。

SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(context, NoopHostnameVerifier.INSTANCE);
HttpClientBuilder builder = HttpClientBuilder.create();
SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(context, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
builder.setSSLSocketFactory(sslConnectionFactory);

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

HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);

builder.setConnectionManager(ccm);

return builder.build();

As manual said, I have replaced library to NoopHostnameVerifier and use it like that:正如手册所说,我已将库替换为 NoopH​​ostnameVerifier 并像这样使用它:

    private static CloseableHttpClient client =
        HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();

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

相关问题 使用非ASCII凭据在httpclient 4.3.x中不起作用 - Use of non-ascii credentials not working in httpclient 4.3.x 使用 4.3.x 重用 HttpClient 连接 - HttpClient connection reusing with 4.3.x 在HttpClient 4.3.x中更正基本身份验证 - Correct Basic Authentication in HttpClient 4.3.x 使用HttpClient 4.3.x,为特定URL执行HttpHead会产生NoHttpResponseException - With HttpClient 4.3.x, executing a HttpHead for a specific URL gives NoHttpResponseException apache httpclient 4.4:HostnameVerifier从4.3.x过渡 - apache httpclient 4.4: HostnameVerifier transition from 4.3.x 如何使用Ajax从网页上使用httpclient 4.3.x获取信息 - how to use httpclient 4.3.x grabbing infomation from web page with ajax 获取Apache httpconents HttpClient 4.3.x OSGi包以解决Apache Karaf 2.3.x的问题 - Problems with getting Apache httpcomponents HttpClient 4.3.x OSGi bundle to work on Apache Karaf 2.3.x 如何在httpClient 4.3.x中强制http客户端不自动处理身份验证挑战? - How to force the http client to not handle the authentication challenges automatically in httpClient 4.3.x? 有一种简单的方法可以手动强制缓存HttpClient 4.3.x绕过缓存吗? - Is there an easy way to manually force a caching HttpClient 4.3.x to bypass the cache? Httpclient已弃用 - Httpclient deprecated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM