10

使用DefaultHttpClient()Apache Commons HTTP Client 时,是否可以在控制台输出中显示完整请求以进行调试?

我的应用程序有问题,我觉得调试它的最简单方法是检查DefaultHTTPClient.

4

5 回答 5

15

您可以像这样获取所有标题:

Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
  String headerName = (String)headerNames.nextElement();
  out.println("" + headerName);
  out.println("" + request.getHeader(headerName));
}

有关更多信息,请参阅本教程

于 2012-09-11T07:32:16.460 回答
9

是的,这是示例代码:

    import java.util.Arrays;
    import org.apache.http.Header;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    ...
    HttpResponse response;
    ...
    HttpGet httpGet = new HttpGet(serviceURL);
    response = httpclient.execute(httpGet);
    ...
    // Print all headers
    List<Header> httpHeaders = Arrays.asList(response.getAllHeaders());        
    for (Header header : httpHeaders) {
        System.out.println("Headers.. name,value:"+header.getName() + "," + header.getValue());
    }
于 2015-08-05T18:58:39.497 回答
8

来自 StackOverflow 上的另一个答案。这可以通过启用 Apache HTTP 客户端的调试日志来轻松完成:

java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST);
java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST);

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "debug");
于 2012-09-12T05:38:04.840 回答
0

当您执行请求时,您正在传递某个 HttpRequest 对象。它有方法getAllHeaders()

于 2012-09-11T07:27:46.027 回答
-1

如果您使用Logback作为您的日志记录框架,请将以下配置添加到您的logback.xml/logback-test.xml文件中:

<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="true">

    <!-- more config -->

    <logger name="org.apache.http" level="DEBUG"/>

    <!-- more config -->

</configuration>

添加此配置后,Logback 的日志附加程序现在将显示有关 HTTP 请求和响应标头的有用的 HttpClient 相关信息,等等。

于 2017-02-21T17:54:59.323 回答