Deprecated Java Httpclient - How Hard Can It Be

Deprecated Java HttpClient - How hard can it be?

Relevant imports:

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;

Usage:

HttpClient httpClient = HttpClientBuilder.create().build();

EDIT (after Jules' suggestion):

As the build() method returns a CloseableHttpClient which is-a AutoClosable, you can place the declaration in a try-with-resources statement (Java 7+):

try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

// use httpClient (no need to close it explicitly)

} catch (IOException e) {

// handle

}

Why I am getting DefaultHttpClient is deprecated?

Use this:

HttpClient httpclient = HttpClientBuilder.create().build();

Refer to this stackoverflow post

Updating deprecated httpclient methods Java

Try this:

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
HttpClient httpClient = httpClientBuilder.build();

This should be the "new" way to do this, according to the API

with httpClient.execute(/*params*/); you should be able to run default as well as custom contexts. See here

Another possible way:

HttpPost httpPost = new HttpPost("/");
httpPost .setProtocolVersion(HttpVersion.HTTP_1_1);

HttpClient is deprecated (android studio, Target=api 22)

Stop using it and use URLConnection instead. It's been 4 years Google recommends this.
http://android-developers.blogspot.be/2011/09/androids-http-clients.html

If you want an external library with a nicer API, you can try OkHttp: http://square.github.io/okhttp/

Deprecated Java HttpClient - How hard can it be?

Relevant imports:

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;

Usage:

HttpClient httpClient = HttpClientBuilder.create().build();

EDIT (after Jules' suggestion):

As the build() method returns a CloseableHttpClient which is-a AutoClosable, you can place the declaration in a try-with-resources statement (Java 7+):

try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

// use httpClient (no need to close it explicitly)

} catch (IOException e) {

// handle

}

Deprecated HttpClient, now using apache-httpclient-4.3.x

I think I have managed to do it, it's working so far.

HttpClientBuilder builder = HttpClientBuilder.create();
KeyStore keyStore = initSSL();

SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(keyStore, new
TrustSelfSignedStrategy()).useTLS().build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

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

PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager(registry);
ccm.setMaxTotal(100);
ccm.setDefaultMaxPerRoute(5);
builder.setConnectionManager(ccm);

BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort(), name),
new UsernamePasswordCredentials(username, password));

builder.setDefaultCredentialsProvider(credsProvider);

RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setSocketTimeout(60000);
builder.setDefaultRequestConfig(requestBuilder.build());

return new HttpContextRequestScopedApacheClientExecutor(builder.build());

HttpClient is reinitialized after each 100 calls

Its currently not possible for you to avoid this re-initialization cost.

API gateway is closing the connection after 100 requests (It'll return Connection: close as one of the response headers). The client has to re-establish the TCP connection and perform a full TLS handshake.

I am from API Gateway, I'll look at if we can do anything about this. Though, I cannot promise anything.

UPDATE: This should be less of an issue now. The number has been increased to 1000.

Replacement for deprecated DefaultHttpClient

Ok I waited for a week and so and did all lots of researches. I think I have found the answer.

I strongly advise for beginners and even professional Android programmers to know the existence of very helpful library called Retrofit:

Extensive document is present at http://square.github.io/retrofit/

The Stack overflow also has samples for almost everything one need to do over a network to contact a remote REST service.

It is better to stop using HttpURLConnection and AsyncTask. Retrofit is way faster and supports all failure situations out of the box.

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

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


Related Topics



Leave a reply



Submit