Java – how to set default headers for all requests in the Apache HTTP client?

For example, the default user agent can be set to:

Solution

Httpclient 4.3 now allows a set of default headers to be configured on the client:

Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE,"application/json");
List<Header> headers = Lists.newArrayList(header);
HttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();
HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL).build();
client.execute(request);

All requests executed by this client will now be sent using the default header I hope it will help

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>