例如,默认用户代理可以设置为:
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,someName);
但是如何设置“Accept”标题?
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,someName);
但是如何设置“Accept”标题?
解决方法
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);
现在,该客户端执行的所有请求都将使用默认标头发送.希望有所帮助.