Reputation: 924
Here is how I am creating HttpClient :
private String userAgent= "Non empty user agent";
private HttpClient httpClient= HttpClientBuilder.create()
.setDefaultCookieStore(new BasicCookieStore())
.setUserAgent(userAgent)
.setDefaultRequestConfig(RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD)
.build())
.setDefaultHeaders(Arrays.asList(
new BasicHeader("Accept-Encoding", "gzip"),
new BasicHeader("Connection", "keep-alive")))
.build();
But when sending some request, the USER AGENT is empty, here is the log :
12:51:33.807 [http-nio-5010-exec-9] DEBUG org.apache.http.headers - http-outgoing-0 >> GET / HTTP/1.1
12:51:33.807 [http-nio-5010-exec-9] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept- Encoding: gzip
12:51:33.807 [http-nio-5010-exec-9] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: www.somehost.com
12:51:33.807 [http-nio-5010-exec-9] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: keep-alive
12:51:33.807 [http-nio-5010-exec-9] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent:
I tried to set the user Agent as default header like this :
.setDefaultRequestConfig(RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD).build())
.setDefaultHeaders(Arrays.asList(
new BasicHeader("Accept-Encoding", "gzip"),
new BasicHeader("Connection", "keep-alive"),
new BasicHeader("User-Agent", userAgent)))
.build();
but same result (user agent is empty).
Here is how I am sending request :
HttpResponse httpResponse = httpClient.execute(new HttpGet(baseUrl));
maven pom dependency for Http Client :
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
Upvotes: 1
Views: 1010
Reputation: 924
The problem was in instantiation order : in fact I am using SpringBoot, and the user agent value is loaded from properties file, here is it :
@Value("${com.myapplication.userAgent}")
private String userAgent;
private HttpClient httpClient= HttpClientBuilder.create()
.setDefaultCookieStore(new BasicCookieStore())
.setUserAgent(userAgent)
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.setDefaultHeaders(Arrays.asList(
new BasicHeader("Accept-Encoding", "gzip"),
new BasicHeader("Connection", "keep-alive"))
)
.build();
The JVM instantiates httpClient before value injection occurs for userAgent, so when instantiating httpClient the userAgent value is null.
Thanks for your mental effort,
Upvotes: 1