Apache HttpComponents

Language: Java

HTTP / Networking

HttpComponents was developed by the Apache Software Foundation to provide a reliable and extensible HTTP client and server implementation for Java. It is widely used for interacting with REST APIs, web scraping, and performing network requests with fine-grained control over connections, cookies, and authentication.

Apache HttpComponents is a set of Java libraries for client-side and server-side HTTP communication. The most commonly used module is HttpClient, which provides a robust and flexible API for sending HTTP requests and handling responses.

Installation

maven: Add dependency in pom.xml: <dependency> <groupId>org.apache.httpcomponents.client5</groupId> <artifactId>httpclient5</artifactId> <version>5.3.1</version> </dependency>
gradle: implementation 'org.apache.httpcomponents.client5:httpclient5:5.3.1'

Usage

HttpComponents HttpClient allows you to send HTTP requests (GET, POST, PUT, DELETE, etc.), handle headers, cookies, authentication, and manage connection pooling. It supports synchronous and asynchronous operations and integrates easily with Java applications.

Simple GET request

import org.apache.hc.client5.http.fluent.Request;

String response = Request.get("https://httpbin.org/get").execute().returnContent().asString();
System.out.println(response);

Performs a simple HTTP GET request and prints the response content.

POST request with JSON payload

import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.ContentType;

String json = "{\"key\":\"value\"}";
String response = Request.post("https://httpbin.org/post")
    .bodyString(json, ContentType.APPLICATION_JSON)
    .execute().returnContent().asString();
System.out.println(response);

Sends a POST request with a JSON payload and prints the response.

Custom headers and authentication

import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.core5.http.io.entity.EntityUtils;

CloseableHttpClient client = HttpClients.custom().build();
HttpGet request = new HttpGet("https://httpbin.org/headers");
request.addHeader("Authorization", "Bearer TOKEN");
String response = client.execute(request, httpResponse -> EntityUtils.toString(httpResponse.getEntity()));
System.out.println(response);

Demonstrates setting custom HTTP headers and using authentication tokens.

Connection pooling

import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(100);
cm.setDefaultMaxPerRoute(20);
CloseableHttpClient client = HttpClients.custom().setConnectionManager(cm).build();

Uses a connection pool to efficiently manage multiple concurrent HTTP requests.

Handling timeouts

import org.apache.hc.client5.http.config.RequestConfig;
RequestConfig config = RequestConfig.custom()
    .setConnectTimeout(5000)
    .setResponseTimeout(5000)
    .build();
HttpGet request = new HttpGet("https://httpbin.org/delay/10");
request.setConfig(config);

Configures connection and response timeouts to prevent hanging requests.

Asynchronous request execution

import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import java.util.concurrent.Future;

CloseableHttpAsyncClient asyncClient = HttpAsyncClients.createDefault();
asyncClient.start();
Future response = asyncClient.execute(SimpleHttpRequest.create("GET", "https://httpbin.org/get"), null);
System.out.println(response.get());

Executes HTTP requests asynchronously to improve performance in high-concurrency scenarios.

Error Handling

IOException: Occurs due to network issues or unreachable endpoints. Check connectivity and URLs.
HttpResponseException: Thrown when the server responds with an unexpected HTTP status code. Handle response codes appropriately.
ConnectionPoolTimeoutException: Occurs when connection pool is exhausted. Increase pool size or reuse clients efficiently.

Best Practices

Reuse HttpClient instances to take advantage of connection pooling.

Always close responses and clients to avoid resource leaks.

Set appropriate timeouts to prevent blocking in network failures.

Handle exceptions like IOException, HttpException to manage request failures.

Use asynchronous clients for scalable, non-blocking HTTP requests.