httpclient-user-agent-header
Custom User-Agent in HttpClient 4
1. Overview
2. Setting User-Agent on the HttpClient
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:
@Test
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");
client.execute(request);
}
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:
@Test
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
@Test
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");
instance.execute(request);
}
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.