使用 apache httpclient 4 显示请求标头时出现问题

发布于 2024-09-14 10:15:18 字数 1272 浏览 7 评论 0原文

我一直在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独守阴晴ぅ圆缺 2024-09-21 10:15:18

要获取所有标头(包括 HTTP 客户端设置的标头),请使用
HttpCoreContext。该类允许读出所有标题。

HttpClient client = HttpClients.createDefault();
HttpCoreContext localContext = new HttpCoreContext();
HttpResponse response = client.execute(request,localContext);

Header[] headers = localContext.getRequest().getAllHeaders();
for (Header header : headers) {
   System.out.println(header.toString());
}

To get all headers including those which the HTTPclient sets, use
HttpCoreContext. This class allows to read out all the headers.

HttpClient client = HttpClients.createDefault();
HttpCoreContext localContext = new HttpCoreContext();
HttpResponse response = client.execute(request,localContext);

Header[] headers = localContext.getRequest().getAllHeaders();
for (Header header : headers) {
   System.out.println(header.toString());
}
流殇 2024-09-21 10:15:18

自 2010 年以来,情况可能已经发生了变化,但是这可以通过使用请求拦截器(在较低级别检查请求)和极其相似的代码来完成。

// So we can get all the headers (not just the ones we explicitly set).        
httpClient.addRequestInterceptor(new HttpRequestInterceptor() {

    public void process(
            final HttpRequest request,
            final HttpContext context) 
            throws HttpException, IOException {

        // Start Debug
        System.out.println("*** Request headers ***");
        Header[] requestHeaders = request.getAllHeaders();
        for(Header header : requestHeaders) {
            System.out.println(header.toString());
        }
        System.out.println("***********************");
        // End Debug
    }

});

就我而言,我得到以下输出(仅显式设置了其中两个)。

*** Request headers ***
Accept: application/xml
Authorization: Basic bmV3Omd1ZXN0
Content-Length: 772
Content-Type: application/xml; charset=UTF-8
Host: rest3api.sifassociation.org:80
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.2.1 (java 1.5)
***********************

愿这对来这里旅行的人有所帮助。

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.

// So we can get all the headers (not just the ones we explicitly set).        
httpClient.addRequestInterceptor(new HttpRequestInterceptor() {

    public void process(
            final HttpRequest request,
            final HttpContext context) 
            throws HttpException, IOException {

        // Start Debug
        System.out.println("*** Request headers ***");
        Header[] requestHeaders = request.getAllHeaders();
        for(Header header : requestHeaders) {
            System.out.println(header.toString());
        }
        System.out.println("***********************");
        // End Debug
    }

});

In my case, I get the following output (having only explicitly set two of these).

*** Request headers ***
Accept: application/xml
Authorization: Basic bmV3Omd1ZXN0
Content-Length: 772
Content-Type: application/xml; charset=UTF-8
Host: rest3api.sifassociation.org:80
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.2.1 (java 1.5)
***********************

May this help those who travel here.

山色无中 2024-09-21 10:15:18

它只会显示您自己设置的请求标头。

如果你想记录 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文