Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 858
Buhake Sindi Avatar asked Apr 21 '14 16:04

Buhake Sindi


Video Answer


3 Answers

I can not comment yet, but here is a small upgrade to herau's answer since it's deprecated since 4.4, maybe someone will find it useful.

SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(context, NoopHostnameVerifier.INSTANCE);
like image 78
pitbbul Avatar answered Oct 17 '22 10:10

pitbbul


As manual said, I have replaced library to NoopHostnameVerifier and use it like that:

    private static CloseableHttpClient client =
        HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
like image 25
Krzysztof Walczewski Avatar answered Oct 17 '22 08:10

Krzysztof Walczewski


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();
like image 16
herau Avatar answered Oct 17 '22 10:10

herau