Which Java HTTP client is better?

agenda

I decided to write HTTP communication in Java, but which client library should I use? I was at a loss because there were many candidates.

Prerequisites

What is

What you have used, what you know

Look for anything else

List the ones that appear in the search candidates of "ok http vs" and "apache http client vs" on Google

Some of them include not only HttpClient but also various functions such as JSON parsing. If you look for it, there seems to be more.

poll

U's choice

U, an early adopter colleague, used OkHttp in his recent app.

Articles on the net (Japanese)

  1. http://qiita.com/Reyurnible/items/33049c293c70bd9924ee → 2016-12-09 Retrofit recommendation

  2. http://kikutaro777.hatenablog.com/entry/2015/07/19/044634 → 2015-07-19 I tried using Unirest

  3. http://vividcode.hatenablog.com/entry/java/google-http-java-client → 2014-11-08 I tried using google-http-java-client

  4. http://qiita.com/kou_hon/items/ddfb3fcb0277103e8b03 → Transfer from Volley to OkHttp

It's not a comparative article, so it's hard to judge, but each atmosphere is conveyed. According to 4, using Volley on Android seems to be tough.

Online articles (overseas)

  1. http://stackoverflow.com/questions/1322335/what-is-the-best-java-library-to-use-for-http-post-get-etc → 2009 Commons HttpClient Recommended. It's just 7 years old, so it's old.

  2. https://www.quora.com/What-is-the-best-both-fast-and-reliable-HTTP-client-library-in-Java → 2014-2015 Unirest, Jersey, Netty, OkHttp, etc. are recommended. It's too loose to be helpful.

Sorting policy

Roughly truncate

Impression so far

The remaining libraries.

Googling with vs

google-http-java-client vs OkHttp → Information cannot be found.

Unirest vs OkHttp → https://android.libhunt.com/project/unirest-java/vs/okhttp The name recognition is overwhelmingly OkHttp

OkHttp vs Jersey → I can't get information.

User number comparison

Due to the large number of articles, it seems that OkHttp has the most users. Jersey is a general word (jersey), so it's a mystery that's hard to google.

Code comparison

Compare code with common features. Sample to set header and body and POST.

OkHttp

OkHttpClient client = new OkHttpClient();
MediaType MIMEType= MediaType.parse("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.create (MIMEType,"{}");
Request request = new Request.Builder().url(url).post(requestBody).build();
Response response = client.newCall(request).execute();

It's a little hard to read because I made too many objects.

google-http-java-client

Response response =
    client
    .preparePost("http://localhost:8080/post")
    .setBody(builder.toString())
    .execute()
    .get();

It's refreshing in the method chain, but it seems to be misunderstood that it is get () at the end even though it is POST.

Unirest

HttpResponse<JsonNode> jsonResponse = Unirest.post("http://localhost:8080/post")
  .header("Content-Type", "application/json")
  .queryString("apiKey", "123")
  .field("param1",  "nitori")
  .field("param2", "1234")
  .asJson();

This is also a method chain. There is nothing special to do.

Jersey

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/post").path("resource");
 
Form form = new Form();
form.param("x", "foo");
form.param("y", "bar");
 
MyJAXBBean bean =
target.request(MediaType.APPLICATION_JSON_TYPE)
    .post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
        MyJAXBBean.class);

Conclusion

OkHttp, which has a large number of users and has established a de facto standard position, is the safest. If you close your eyes to being a minor, Unirest seems to be the most convenient and clean implementation in terms of code.

Something more

For more in-depth requirements, performance, cookie management, retries, multithreading, etc. need to be considered.

bonus

Java9 may or may not have HttpClient in the standard library. https://thoughtfulsoftware.wordpress.com/2016/09/12/java-9-the-new-httpclient/ But it's still not available on Android.

Recommended Posts

Which Java HTTP client is better?
Which is better, Kotlin or Java in the future?
Java HTTP Client API timeout setting
What is java
What is Java <>?
Java mqtt client
What is Java
Set HTTP Keep Alive Timeout in Java HTTP Client
The question of which is better, if or switch
What is Java Encapsulation?
What is Java technology?
What is Java API-java
[Java] What is flatMap?
[Java] What is JavaBeans?
[Java] What is ArrayList?
This is all you need WebSocket: Server = Java, Client = Java, JavaScript
Java: The problem of which is faster, stream or loop
Which is better, generalization or delegation, when there are overlaps?
What is Java Assertion? Summary.
What is a Java collection?
[Windows] Java code is garbled
[Java] What is jaee j2ee?
[Java] What is class inheritance?
[Java] Server Client Communication 1 (Unfinished)
[Java basics] What is Class?
What is java escape analysis?
Java is the 5th day