Difference between Httpclientbuilder.create(). build(), Httpclients.createDefault() and Defaulthttpclient

Asked

Viewed 754 times

3

When creating an HTTP connection using:

HttpClientBuilder.create().build(), HttpClients.createDefault()

or:

DefaultHttpClient.

What’s the difference?

private static CloseableHttpClient httpClient = HttpClientBuilder.create().build();

private static CloseableHttpClient httpClient = HttpClients.createDefault();

private static DefaultHttpClient httpClient = new DefaultHttpClient();

And which is the best one to use?

1 answer

2


Come on,

  1. HttpClients is an interface to assist you in creating classes that inherit from the abstract class CloseableHttpClient (In this case, it implements both the interface HttpClient how much Closeable);
  2. HttpClientBuilder is an auxiliary class using the design standard BUILDER (Or Factory, if you prefer) to create classes that inherit from CloseableHttpClient;
  3. DefaultHttpClient is a class that inherits from AbstractHttpClient, in which that inherits from CloseableHttpClient.

In addition, the method build() and createDefault() generate a class DefaultHttpClient. In short, all the same thing only in different ways to implement.

I hope I’ve helped. :-)

  • Thanks for your help. Just one more question you explained that build() and createDefault() generate a Defaulthttpclient class and when I use Defaulthttpclient directly it is deprecated. So for me not to have this warning the most suitable and I use one of the other two methods?

  • That’s right @Davidsoares!

  • Got it thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.