Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Practical Apache HttpClient Implementation Guide

Tech Nov 2 1

Basic HTTP GET Request

Executing a simple HTTP GET request involves creating an CloseableHttpClient instance, defining the request method, and processing the response. It is crucial to ensure resources are closed properly to avoid memory leaks.

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class SimpleGetExample {
    public static void main(String[] args) throws Exception {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet("http://httpbin.org/get");
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                System.out.println("Response status: " + response.getStatusLine());
                String responseBody = EntityUtils.toString(response.getEntity());
                System.out.println("Response body: " + responseBody);
            }
        }
    }
}

Multipart File Upload

Uploading files requires building a multipart entity. The MultipartEntityBuilder allows adding binary data, form fields, or files to the request body. The server-side implementation can leverage Servlet 3.0 annotations to handle the parts.

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.File;

public class FileUploadClient {
    public static void uploadFile(String url, File file) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost uploadFile = new HttpPost(url);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addBinaryBody("file", file);
            uploadFile.setEntity(builder.build());
            httpClient.execute(uploadFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Server-side handler using javax.servlet.annotation.MultipartConfig:

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.Part;

@MultipartConfig
public class FileUploadServlet extends HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) {
        for (Part part : request.getParts()) {
            String fileName = part.getSubmittedFileName();
            part.write("/storage/" + fileName);
        }
    }
}

Request Configuration and Timeouts

HttpClient allows fine-grained control over request behavior through RequestConfig. Critical settings include connection timeout, socket timeout, and connection request timeout.

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;

RequestConfig config = RequestConfig.custom()
        .setConnectTimeout(5000)
        .setSocketTimeout(5000)
        .setConnectionRequestTimeout(5000)
        .build();
HttpGet httpGet = new HttpGet("http://example.com/resource");
httpGet.setConfig(config);

Protocol Interceptors

Interceptors allow for the modification of requests and responses before they are sent or processed. They are useful for adding default headers or logging. The HttpClientContext can be used to pass data between interceptor logic.

import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;

CloseableHttpClient httpClient = HttpClients.custom()
        .addInterceptorLast((HttpRequestInterceptor) (request, context) -> {
            request.addHeader("X-Custom-Header", "Value");
        }).build();

Low-Level Connection Management

For specific scenarios, managing connections directly is necessary. The BasicHttpClientConnectionManager handles one connection at a time, requiring manual acquisition and release.

import org.apache.http.HttpHost;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.http.HttpClientConnection;
import org.apache.http.conn.routing.HttpRoute;
import java.util.concurrent.TimeUnit;

HttpClientConnectionManager cm = new BasicHttpClientConnectionManager();
HttpRoute route = new HttpRoute(new HttpHost("localhost", 8080));
HttpClientConnection conn = cm.requestConnection(route, null).get(5, TimeUnit.SECONDS);
try {
    if (!conn.isOpen()) {
        cm.connect(conn, route, 1000, null);
    }
    // Use connection
} finally {
    cm.releaseConnection(conn, null, 1, TimeUnit.MINUTES);
}

Connection Pooling Strategy

To handle concurrent requests efficiently, PoolingHttpClientConnectionManager reuses connections, reducing the overhead of establishing new connections for each request. Limits can be set for total connections and per-route.

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

PoolingHttpClientConnectionManager poolManager = new PoolingHttpClientConnectionManager();
poolManager.setMaxTotal(200); // Max total connections
poolManager.setDefaultMaxPerRoute(20); // Max connections per route

CloseableHttpClient client = HttpClients.custom()
        .setConnectionManager(poolManager)
        .build();

Connection Keep-Alive Customization

By default, HttpClient attempts to keep connections alive. A custom ConnectionKeepAliveStrategy can define specific durations based on the target host or response headers if the server does not specify a timeout.

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.protocol.HttpContext;

CloseableHttpClient client = HttpClients.custom()
        .setKeepAliveStrategy((HttpResponse response, HttpContext context) -> {
            // Override keep-alive for specific hosts or default to 30 seconds
            return 30 * 1000; 
        })
        .build();

SSL Context with PKCS12 Certificates

Secure communication often requires mutual SSL authentication. A custom SSLContext can be created using a PKCS12 keystore to load client certificates.

import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;

public class SslClient {
    public static CloseableHttpClient createSslClient(String certPath, String password) throws Exception {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        try (FileInputStream instream = new FileInputStream(new File(certPath))) {
            keyStore.load(instream, password.toCharArray());
        }
        SSLContext sslContext = SSLContexts.custom()
                .loadKeyMaterial(keyStore, password.toCharArray())
                .build();
        SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
        return HttpClients.custom()
                .setSSLSocketFactory(socketFactory)
                .build();
    }
}
Tags: Java

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.