4

I am trying to create the HTTPClient for building a framework using Apache HTTPClient in Java for REST webservices.

Here I found that we can create the client using the below two ways. I want to know the difference between them.

1.HTTPCLientBuilder.create().build()

2.HTTPClients.custom().build()

2 Answers2

1

The answer is on SO, just a bit hidden. Reposting euphoria99's answer from this question:

HttpClient (interface)

implemented by:

CloseableHttpClient - ThreadSafe.

DefaultHttpClient - ThreadSafe BUT deprecated, use HttpClientBuilder instead.
HttpClientBuilder - NOT ThreadSafe, BUT creates ThreadSafe CloseableHttpClient.

Use to create CUSTOM CloseableHttpClient.
HttpClients - NOT ThreadSafe, BUT creates ThreadSafe CloseableHttpClient.

Use to create DEFAULT or MINIMAL CloseableHttpClient.

So when you write .custom() and then don't set any custom things, there is, aparently, no differnce

Andrejs
  • 10,803
  • 4
  • 43
  • 48
0

If you look at the source of HttpClients you will see:

/**
 * Creates builder object for construction of custom
 * {@link CloseableHttpClient} instances.
 */
public static HttpClientBuilder custom() {
    return HttpClientBuilder.create();
}

So, to answer the question, the only difference is one extra call in the stack as one uses the other.

Talking in general:

  • HttpClients has predetermined "configurations" for creating HttpClient instances.
  • HttpClientBuilder is for building new HttpClient instances that you can customize during the building process and it is used by HttpClients in some of its methods.
PhoneixS
  • 10,574
  • 6
  • 57
  • 73