1. Overview
In this tutorial, we will look at how we can set the user agent for Apache HttpClient 4.
2. Default User Agent
Apache 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 library version, such as 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 call 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 use 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 implementation to fit 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 to set the User-Agent header for Apache HttpClient.
As always, the source code for all examples is available on GitHub.