Java 如何禁用来自 apache httpclient 4 的默认请求标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23291209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I disable default request headers from apache httpclient 4?
提问by Sean Nguyen
I am using apache common httpclient 4.3.3 to make http 1.0 request. Here is how I make the request
我正在使用 apache common httpclient 4.3.3 来发出 http 1.0 请求。这是我提出请求的方式
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
post.setProtocolVersion(new ProtocolVersion("HTTP", 1, 0));
// trying to remove default headers but it doesn't work
post.removeHeaders("User-Agent");
post.removeHeaders("Accept-Encoding");
post.removeHeaders("Connection");
post.setEntity(new ByteArrayEntity(ba) );
HttpResponse response = client.execute(post);
However, i can see that there are other headers automatically added to my request to the server like
但是,我可以看到还有其他标头自动添加到我对服务器的请求中,例如
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.3.3 (java 1.5)
Accept-Encoding: gzip,deflate
How can I tell httpclient not to include any other headers? I tried to removed those headers with post.removeHeaders(xxxx) but it doesn't work. Can you show me how?
如何告诉 httpclient 不包含任何其他标头?我试图用 post.removeHeaders(xxxx) 删除那些标题,但它不起作用。你能告诉我怎么做吗?
Thanks,
谢谢,
回答by Stephan
I think you could add your implementation of HttpRequestInterceptor
with client.addRequestInterceptor()
or(better)
remove all interceptors that add headers to the request (RequestUserAgent
, RequestDefaultHeaders
, RequestClientConnControl
, RequestAcceptEncoding
, ...).
我认为您可以添加HttpRequestInterceptor
withclient.addRequestInterceptor()
或(更好)的实现,
删除所有向请求添加标头的拦截器(RequestUserAgent
, RequestDefaultHeaders
, RequestClientConnControl
, RequestAcceptEncoding
, ...)。
Removing them is also easy:
删除它们也很容易:
client.removeRequestInterceptorByClass(RequestAcceptEncoding.class);
回答by ok2c
CloseableHttpClient hc = HttpClients.custom()
.setHttpProcessor(HttpProcessorBuilder.create().build())
.build();
The code snippet above demonstrates how to create an HttpClient instance with an empty (no-op) protocol processor, which guarantees no request headers will ever be added to outgoing messages executed by such client
上面的代码片段演示了如何创建一个带有空(无操作)协议处理器的 HttpClient 实例,这保证不会将请求标头添加到由此类客户端执行的传出消息中
回答by Cheemin
If you call HttpClientBuilder.create()
, you will have a httpClientBuilder.
And httpClientBuilder has a lot config for default headers and this will be used to make intercepters( ex: RequestAcceptEncoding ).
如果您调用HttpClientBuilder.create()
,您将拥有一个 httpClientBuilder。并且 httpClientBuilder 有很多默认标头的配置,这将用于制作拦截器(例如:RequestAcceptEncoding)。
For example, RequestAcceptEncoding, which implements HttpRequestInterceptor, makes Accept-Encoding: gzip,deflate
header when HttpProcessor.process() is invoked.
And httpProcessor.process() will be invoked just before invoking
final CloseableHttpResponse response = this.requestExecutor.execute(route, request, context, execAware);
例如,实现 HttpRequestInterceptor 的 RequestAcceptEncodingAccept-Encoding: gzip,deflate
在调用 HttpProcessor.process() 时制作标头。并且 httpProcessor.process() 将在调用之前被调用
final CloseableHttpResponse response = this.requestExecutor.execute(route, request, context, execAware);
You can see this code at org.apache.http.impl.execchain.ProtocolExec of httpclient-4.3.6 line 193.
您可以在 httpclient-4.3.6 第 193 行的 org.apache.http.impl.execchain.ProtocolExec 看到此代码。
If you want to remove Accept-Encoding: gzip,deflate
, call HttpClientBuilder.disableContentCompression()
like below.
如果要删除Accept-Encoding: gzip,deflate
,请按HttpClientBuilder.disableContentCompression()
如下方式调用。
HttpClient client = HttpClientBuilder.create().disableContentCompression().build();
In short, HttpClientBuilder has a lot of flags to disable/enable HttpRequestInterceptor. If you disable/enable those HttpRequestInterceptor, you can exclude/include default headers.
简而言之,HttpClientBuilder 有很多标志来禁用/启用 HttpRequestInterceptor。如果禁用/启用那些 HttpRequestInterceptor,则可以排除/包含默认标头。
Sorry for my poor English, and hope you get what I mean.
对不起,我的英语不好,希望你明白我的意思。
回答by Brian G
You want to do your 'cleanup' at the end, after HttpClient is done modifying the request. You can do this by calling addInterceptorLast on HttpClientBuilder as below.
在 HttpClient 完成修改请求后,您希望在最后进行“清理”。您可以通过在 HttpClientBuilder 上调用 addInterceptorLast 来做到这一点,如下所示。
HttpClient client = HttpClientBuilder.create().addInterceptorLast(
new HttpRequestInterceptor() {
public void process(HttpRequest request, HttpContext context){
request.removeHeaders("Host");
request.removeHeaders("Connection");
}
}
).build();
I create an anonymous class implementing HttpRequestInterceptor. Take whatever header modifications you need done before the request is sent, and put them in the process method.
我创建了一个实现 HttpRequestInterceptor 的匿名类。在发送请求之前进行任何您需要完成的标头修改,并将它们放入 process 方法中。