Mihkel L.
Mihkel L.

Reputation: 1573

HTTPClient and PostMethod

So I ran into a problem and the service support asked me to support them with the HTML. So I need to get the equivalent HTML that is generated by

HttpClient client = new HttpClient();
PostMethod post = new PostMethod( "https://www.url.to/post-to" );
NameValuePair[] params = {
                new NameValuePair( "NAME", name ),
                new NameValuePair( "EMAIL", email ),
for( NameValuePair param : params ){
   post.setParameter(param.getName(), param.getValue());
}

Is it possible to get it as HTML or get the request sent as a string, with all the headers etc?

Upvotes: 0

Views: 1262

Answers (2)

Predrag Maric
Predrag Maric

Reputation: 24433

If you use some logging framework, Apache HttpClient can be configured to log requests and responses which go through it, see Wire logging. Judging by the documentation, if logging framework is properly setup, this should be enough to enable it

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.commons.httpclient", "debug");

EDIT

These settings are ok if you use Apache Commons Logging as logging framework. Above mentioned link also contains examples for setting this up for log4j and java.util.logging.

Upvotes: 1

RealSkeptic
RealSkeptic

Reputation: 34628

You could write an equivalent HTML form, that sends the same two parameters to the same URL.

However, if you're opening a support ticket for a problem caused by this connection, an HTML will probably not be very helpful, because a browser may be sending different headers to the URL even if the request body is the same.

You can get the request headers from the post variable, using post.getRequestHeaders(). It returns an array of type Header[], which you'll have to translate into strings. The message body is probably easy to reconstruct from the parameters by urlencoding the values. The body is always in the form NAME=urlencodedname&EMAIL=urlencodedemail.

Upvotes: 0

Related Questions