使用 apache httpclient 4 显示请求标头时出现问题
我一直在尝试使用 HttpClient 4 检索 HttpMethod 发送的标头,但没有成功...
这是我的代码:
HttpClient httpClient = new DefaultHttpClient();
HttpParams httpParams = httpClient.getParams();
HttpGet httpGet = new HttpGet("http://www.google.fr");
HttpResponse response = httpClient.execute(httpGet);
log.info("*** Request headers ***");
Header[] requestHeaders = httpGet.getAllHeaders();
for(Header header : requestHeaders) {
log.info(header.toString());
}
log.info("***********************");
log.info("*** reponse ***");
log.info(response.getStatusLine());
Header[] headers = response.getAllHeaders();
for(Header header : headers) {
log.info(header.toString());
}
但结果是:
00:27:57,368 INFO - *** Request headers ***
00:27:57,368 INFO - ***********************
00:27:57,368 INFO - *** reponse ***
00:27:57,368 INFO - HTTP/1.1 200 OK
00:27:57,368 INFO - Date: Sun, 15 Aug 2010 22:28:09 GMT
00:27:57,368 INFO - Expires: -1
00:27:57,368 INFO - Cache-Control: private, max-age=0
00:27:57,368 INFO - Content-Type: text/html; charset=ISO-8859-1
00:27:57,368 INFO - Set-Cookie:
[..]
又名响应标头很好,但不是请求的标头。 (如果我将日志请求标头块移到执行语句之前,结果相同)。
(不,我不想简单地看到它们,因此将日志级别设置为调试是不可接受的)
任何人都可以提供帮助吗?
I've been trying to retrieve the headers sent by a HttpMethod, using HttpClient 4, but without any success...
here is my code :
HttpClient httpClient = new DefaultHttpClient();
HttpParams httpParams = httpClient.getParams();
HttpGet httpGet = new HttpGet("http://www.google.fr");
HttpResponse response = httpClient.execute(httpGet);
log.info("*** Request headers ***");
Header[] requestHeaders = httpGet.getAllHeaders();
for(Header header : requestHeaders) {
log.info(header.toString());
}
log.info("***********************");
log.info("*** reponse ***");
log.info(response.getStatusLine());
Header[] headers = response.getAllHeaders();
for(Header header : headers) {
log.info(header.toString());
}
but the result is :
00:27:57,368 INFO - *** Request headers ***
00:27:57,368 INFO - ***********************
00:27:57,368 INFO - *** reponse ***
00:27:57,368 INFO - HTTP/1.1 200 OK
00:27:57,368 INFO - Date: Sun, 15 Aug 2010 22:28:09 GMT
00:27:57,368 INFO - Expires: -1
00:27:57,368 INFO - Cache-Control: private, max-age=0
00:27:57,368 INFO - Content-Type: text/html; charset=ISO-8859-1
00:27:57,368 INFO - Set-Cookie:
[..]
Aka the response headers are good, but not the request's. ( Same result if I move the log request headers block before the execute statement ).
(and NO, I dont want to simply see them, so setting the log level to debug isnt acceptable )
Anyone can help ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要获取所有标头(包括 HTTP 客户端设置的标头),请使用
HttpCoreContext。该类允许读出所有标题。
To get all headers including those which the HTTPclient sets, use
HttpCoreContext. This class allows to read out all the headers.
自 2010 年以来,情况可能已经发生了变化,但是这可以通过使用请求拦截器(在较低级别检查请求)和极其相似的代码来完成。
就我而言,我得到以下输出(仅显式设置了其中两个)。
愿这对来这里旅行的人有所帮助。
Things may have changed since 2010, however this can be done by using a request interceptor (to inspect the request at a lower level) with amazingly similar code.
In my case, I get the following output (having only explicitly set two of these).
May this help those who travel here.
它只会显示您自己设置的请求标头。
如果你想记录 HttpClient 设置的请求头,那么你需要通过 Commons Logging< 配置 HttpClient 的内置日志记录/a>.另请参阅此文档。
作为替代方案,您还可以使用外部工具,例如 Fiddler。
It will only display the request headers you've set yourself.
If you want to log the request headers which HttpClient has set, then you need to configure HttpClient's builtin logging by Commons Logging. Also see this document.
As an alternative, you can also use an external tool like Fiddler.