Using Apache HTTPClient - how can one see the raw request string before it's being sent?

走远了吗. 提交于 2019-11-28 08:20:41

You can set some environment variables for Apache HttpClient (example tested for 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");

There are also some more variables for debugging:

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");

Try this:

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

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

to retrieve the URI

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

to retrieve the parameters in POST

method.getRequestEntity().writeRequest(System.out);
org.apache.http.client.fluent.Request#viaProxy 

This method can make your request pass through proxy server, so your can launch a local proxy server, for example Fiddler, so this debugging proxy can show the details of http request and response.

Try enabling DEBUG mode in your logging configurations, if you're using log4j you can add this to the log4j.xml file of your project

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

You will have the full request headers, params, body, etc, logged in your logs files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!