1. Overview

This quick tutorial will show how to send a custom User-Agent header using Apache HttpClient 4.

2. Setting User-Agent on the HttpClient

2.1. Before HttpClient 4.3

When working with older versions of Http Client (pre 4.3), setting the value of the User-Agent was done via a low level API:

client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 Firefox/26.0");

The same can be done via a higher level API as well – without dealing with the raw http.useragent property:

HttpProtocolParams.setUserAgent(client.getParams(), "Mozilla/5.0 Firefox/26.0");

A full example would look like this:

public void whenClientUsesCustomUserAgent_thenCorrect() 
  throws ClientProtocolException, IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpProtocolParams.setUserAgent(client.getParams(), "Mozilla/5.0 Firefox/26.0");

    HttpGet request = new HttpGet("http://www.github.com");

2.2. After HttpClient 4.3

In recent version of the Apache client (after 4.3), the same is achieved in a much cleaner way, via the new fluent APIs:

public void whenRequestHasCustomUserAgent_thenCorrect() 
  throws ClientProtocolException, IOException {
    HttpClient instance = HttpClients.custom().setUserAgent("Mozilla/5.0 Firefox/26.0").build();
    instance.execute(new HttpGet("http://www.github.com"));

3. Setting User-Agent on Individual Requests

A custom User-Agent header can also be set on individual requests, not on the entire HttpClient:

public void givenDeprecatedApi_whenRequestHasCustomUserAgent_thenCorrect() 
  throws ClientProtocolException, IOException {
    HttpClient instance = HttpClients.custom().build();
    HttpGet request = new HttpGet(SAMPLE_URL);
    request.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 Firefox/26.0");

4. Conclusion

This article illustrated how you can use the HttpClient to send requests with custom User-Agent header – for example to simulate the behavior of a specific browser.

The implementation of all these examples and code snippets can be found in my github project – this is an Eclipse based project, so it should be easy to import and run as it is.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Comments are closed on this article!