【问题标题】:How to set the default header for all requests in apache http client?如何为 apache http 客户端中的所有请求设置默认标头?
【发布时间】:2013-09-13 11:59:15
【问题描述】:

例如,可以将默认用户代理设置为: client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, someName);
但是如何设置“接受”标头?

【问题讨论】:

标签: java httpclient


【解决方案1】:

HttpClient 4.3 现在允许在客户端本身配置一组默认标头:

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);

现在,该客户端执行的所有请求都将使用默认标头发送。 希望有帮助。

【讨论】:

    猜你喜欢
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 2016-06-20
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多