1. Overview

In this tutorial, we’re going to look at how we can set the user agent for Apache HttpClient.

2. Default User Agent

HttpClient sends a default User-Agent header with every request.

public void executeAndDefaultUserAgent() throws Exception {
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        final HttpGet httpGet = new HttpGet(GET_URL);
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            handleResponse(response);
        }
    }
}

This value changes according to the used library version like Apache-HttpClient/4.5.8 (Java/1.8.0_191).

3. Set Custom User Agent

We can also set a custom User-Agent header using the HttpClientBuilder class:

public void executeAndSetUserAgent() throws Exception {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create()
      .setUserAgent("HttpClient Custom User Agent")
      .build()) {
        final HttpGet httpGet = new HttpGet(GET_URL);
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            handleResponse(response);
        }
    }
}

Here, we’re calling the setUserAgent() method of HttpClientBuilder with “HttpClient Custom User Agent”.

4. Set User Agent Dynamically

When we select the user agent with setUserAgent(), all requests will use the same user agent value. To use a different value for each request, we must make use of HttpRequestInterceptor.

public void executeAndSetUserAgentWithInterceptor() throws Exception {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create()
      .addInterceptorLast(new HttpRequestInterceptor() {
          private final String[] userAgents = new String[]{"UserAgent1", "UserAgent2", "UserAgent3"};
          private final Random random = new Random();
          @Override
          public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
              httpRequest.setHeader("User-Agent", userAgents[random.nextInt(3)]);
          }
      })
      .build()) {
        final HttpGet httpGet = new HttpGet(GET_URL);
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            handleResponse(response);
        }
    }
}

Our HttpRequestInterceptor implementation selects a random value from the userAgents array. Although we’re using a fixed length array, we can change the actual implementation according to our needs.

5. Disable User Agent

Additionally, we can disable the user agent, so that HttpClient sends no User-Agent header.

public void executeAndDisableUserAgent() throws Exception {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create()
      .disableDefaultUserAgent()
      .build()) {
        final HttpGet httpGet = new HttpGet(GET_URL);
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            handleResponse(response);
        }
    }
}

For this purpose, we’re using the disableDefaultUserAgent() method of HttpClientBuilder.

6. Summary

In this tutorial, we’ve investigated how we can set the User-Agent header for Apache HttpClient.

As always, the source code for all examples is available on Github.

Leave a Reply

Close Menu