如何在apache http客户端中为所有请求设置默认头? [英] How to set the default header for all requests in apache http client?

查看:1533
本文介绍了如何在apache http客户端中为所有请求设置默认头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,默认用户代理可以设置为:
client.getParams()。setParameter(CoreProtocolPNames.USER_AGENT,someName);

但是如何设置Accept标题?

For example the default user agent could be set like: client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, someName);
But how to set the "Accept" header?

推荐答案

HttpClient 4.3现在允许配置一个默认标题集合客户端本身:

HttpClient 4.3 now allows configuring a collection of default headers on the client itself:

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

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

Now, all requests executed by that client will be send with the default headers. Hope that helps.

这篇关于如何在apache http客户端中为所有请求设置默认头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆