21

To make a http request, there're some APIs alternative in JAVA, such as Apache HttpClient and Okhttp. Apache HttpClient is mature and widely used, and Okhttp seems to be more and more popular(I'm not sure).

What I wondered is, which is better, or does Apache HttpClient has some feature that Okhttp doesn't have, or the other way round? Mostly, I'm talking in server side, it's also very nice to talk about using in Android App.

I hope some points could be listed, therefore I can figure out the differences between Apache HttpClient and Okhttp.

Reference:

There's a question between URLConnection and HttpClient: URLConnection or HTTPClient : Which offers better functionality and more efficiency?

Community
  • 1
  • 1
Coderec
  • 223
  • 1
  • 2
  • 7
  • The overhead is on the network and at the server. Not in the client API. – user207421 Dec 06 '19 at 01:00
  • "The overhead is on the network and at the server. Not in the client API." @user207421 It's not true. I can confirm that one http client may be up to 10x times (and even more in special cases) more efficient than another. E.g., you can see this results: https://github.com/ok2c/httpclient-benchmark/wiki – Mikhail Ionkin Nov 05 '22 at 12:29

2 Answers2

18

I suggest to use okhttp. Here is the reason: https://github.com/square/okhttp/issues/3472

OkHttp has HTTP/2, a built-in response cache, web sockets, and a simpler API. It’s got better defaults and is easier to use efficiently. It’s got a better URL model, a better cookie model, a better headers model and a better call model. OkHttp makes canceling calls easy. OkHttp has carefully managed TLS defaults that are secure and widely compatible. Okhttp works with Retrofit, which is a brilliant API for REST. It also works with Okio, which is a great library for data streams. OkHttp is a small library with one small dependency (Okio) and is less code to learn. OkHttp is more widely deployed, with a billion Android 4.4+ devices using it internally.

Al3xC
  • 194
  • 2
  • 7
  • The above is true, and both libraries are pretty much equivalent. Which one to pick is just a matter of spoiled taste – J11 Dec 06 '19 at 02:06
2

I used both. Okhttp is easier but it has limitations. So you can not change timeout like configuration after creating a singleton connection. It is a serious limitation.

Sharofiddin
  • 340
  • 2
  • 14
  • 5
    That's not true anymore. An `OkHttpClient` can be cloned easily (see this answer: https://stackoverflow.com/a/47155363/9864539) and in the cloned instance you can change the timeout. – Ricky Sixx Feb 05 '21 at 15:41
  • 1
    @RickySixx as you know I said Singleton and you are talking about cloning. – Sharofiddin May 30 '22 at 12:39