Using Apache HTTPClient - how can one see the raw

2019-01-23 04:37发布

问题:

For debugging purposes, I'd like to see the raw request that is going to be sent. Is there a way to get this without a HTTP monitor directly from the API of HttpPost or HttpClient?

I found some "almost" duplicate questions, but not for this particular one

回答1:

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


回答2:

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


回答3:

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.