Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java apache httpclient 4显示请求头时出现问题_Java_Http_Httpclient - Fatal编程技术网

Java apache httpclient 4显示请求头时出现问题

Java apache httpclient 4显示请求头时出现问题,java,http,httpclient,Java,Http,Httpclient,我一直在尝试使用HttpClient 4检索HttpMethod发送的头,但没有任何成功 这是我的密码: HttpClient httpClient = new DefaultHttpClient(); HttpParams httpParams = httpClient.getParams(); HttpGet httpGet = new HttpGet("http://www.google.fr"); HttpResponse response = httpClient.execute(h

我一直在尝试使用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: 

[..]
也就是说,响应头是好的,但不是请求的。(如果在execute语句之前移动log request headers块,结果相同)

(不,我不想简单地看到它们,因此将日志级别设置为debug是不可接受的)


有人可以帮忙吗?

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

如果要记录HttpClient已设置的请求头,则需要通过配置HttpClient的内置日志记录。另见


作为替代,您还可以使用外部工具,如。

自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
    }

});
在我的例子中,我得到以下输出(只显式地设置了其中的两个)


希望这能帮助那些在这里旅行的人。

要获取所有标题,包括HTTPclient设置的标题,请使用
HttpCoreContext。此类允许读取所有标题。


谢谢你的回答,但我说更改日志级别是不可接受的:我需要存储发送的标题。您确定在执行请求之前甚至之后都无法获取它们吗?虽然这个答案很完美,但我认为不记录日志就解决问题是一个更好的解决方案(请参阅投票最多的答案)。(我没有投票支持这个答案。)
*** 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)
***********************
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());
}