使用Apache HTTPClient - 如何在发送之前看到原始请求字符串?

问题描述 投票:16回答:4

出于调试目的,我希望看到将要发送的原始请求。有没有办法在没有HTTP监视器的情况下直接从HttpPostHttpClient的API获取?

我发现了一些“几乎”重复的问题,但不适用于这个问题

apache-commons-httpclient
4个回答
29
投票

您可以为Apache HttpClient设置一些环境变量(示例测试为4.3.2)。

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.org.apache.http.wire", "DEBUG");

还有一些用于调试的变量:

System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.impl.conn", "DEBUG");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.impl.client", "DEBUG");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.client", "DEBUG");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "DEBUG");

0
投票

试试这个:

HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);

method.setParameter(...., ....);

检索URI

System.out.println("getUri: " + method.getURI());

在POST中检索参数

method.getRequestEntity().writeRequest(System.out);

0
投票
org.apache.http.client.fluent.Request#viaProxy 

此方法可以使您的请求通过代理服务器,因此您可以启动本地代理服务器,例如Fiddler,因此此调试代理可以显示http请求和响应的详细信息。


0
投票

尝试在日志配置中启用DEBUG模式,如果您使用log4j,可以将其添加到项目的log4j.xml文件中

<root>
    <priority value="DEBUG" />
    <appender-ref ref="FILE" />
</root>

您将在日志文件中记录完整的请求标题,参数,正文等。

© www.soinside.com 2019 - 2024. All rights reserved.