使用Apache Commons HTTP Client中的DefaultHttpClient()
时,是否可以在控制台输出中显示完整请求以进行调试?
我的应用程序出现问题,我觉得最简单的调试方法是检查DefaultHTTPClient
发送的所有数据。
答案 0 :(得分:15)
你可以得到这样的所有标题:
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = (String)headerNames.nextElement();
out.println("" + headerName);
out.println("" + request.getHeader(headerName));
}
有关详细信息,请参阅this tutorial。
答案 1 :(得分:6)
来自StackOverflow的另一个答案。这可以通过启用Apache HTTP Client的调试日志记录来轻松完成:
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");
答案 2 :(得分:6)
是的,这是示例代码:
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());
}
答案 3 :(得分:0)
当您执行请求时,您正在传递某个HttpRequest对象。它有方法getAllHeaders()
答案 4 :(得分:-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相关信息等。