In this article, we learn how to use Apache HttpClient. Let’s get started.


Table of contents


Introduction to Apache HttpClient

The first version of Apache HttpClient is 4.0-alpha1, it was released at Jul 18, 2007. And nowadays, this library is still supported by Apache with the newest version - 4.5.11.

Apache HttpClient has the same functionality with HttpURLConnection is to communicate with other system based on HTTP protocol.

Spring RestTemplate and Apache HttpClient API work at different levels of abstraction.

  • Spring RestTemplate is superior to the HttpClient and take care of the tranformation from JSON or XML to Java objects.

  • The Apache HttpClient takes care of all low level details of communication via Http.

But we also can use some other http client libraries such as OkHttp, Netty.

In order to use Apache HttpClient APIs, we can add depedency into pom.xml file:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
</dependency>


Source code

In order to understand how to implement code by using Apache HttpClient, we can check out source code in Apache HttpClient Utils.


Benefits and Drawbacks

  1. Benefits

    • Large and extensive APIs

    • Supports cookie handling, authentication and connection management

    • More suitable for web browser and other web applications, because of its large size

  2. Drawbacks

    • When using in Android app, Google blog lists few bugs, we can reference to this link.

    • Does not support HttpResponseCache mechanism, hence leading to increased network usage and battery consumption


Wrapping up


Refer:

https://hc.apache.org/httpcomponents-client-4.5.x/examples.html

https://www.baeldung.com/httpclient-multipart-upload

https://www.baeldung.com/httpclient-connection-management

https://www.baeldung.com/httpclient-post-http-request

https://www.baeldung.com/httpclient-guide

https://springframework.guru/using-resttemplate-with-apaches-httpclient/

https://www.journaldev.com/7146/apache-httpclient-example-closeablehttpclient

https://www.javaguides.net/2018/10/apache-httpclient-get-post-put-and-delete-methods-example.html