11.7. Making an HTTP POST Request

Problem

You need to supply parameters to a script or servlet using HTTP POST.

Discussion

Create a PostMethod and call setParameter( ) or addParameter() before you execute the method. The PostMethod will send a request with a Content-Type header of application/x-www-form-urlencoded, and the parameters will be sent in the request body. The following example demonstrates the use of PostMethod to send parameters:

               import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

HttpClient client = new HttpClient( );
        
// Create POST method
String url = "http://www.discursive.com/cgi-bin/jccook/param_list.cgi";
PostMethod method = new PostMethod( url );

// Set parameters on POST    
               method.setParameter( "test1", "Hello World" );
               method.addParameter( "test2", "This is a Form Submission" );
               method.addParameter( "Blah", "Whoop" );
               method.addParameter( new NameValuePair( "Blah", "Whoop2" ) );

// Execute and print response
client.executeMethod( method );
String response = method.getResponseBodyAsString( );
System.out.println( response );

method.releaseConnection( );

The param_list.cgi CGI script echoes all parameters received, and from the following output, you can see that three parameters were supplied to this script:

These are the parameters I received:

test1:
  Hello World
test2:
  This is a Form Submission
Blah:
  Whoop
  Whoop2

Discussion

The previous example sent the parameters in the request body. The request created by the previous example is shown below:

POST /cgi-bin/jccook/param_list.cgi HTTP/1.1[
][
]
User-Agent: Jakarta Commons-HttpClient/3.0final[
][
]
Host: www.discursive.com[
][
]
Content-Length: 72[
][
]
Content-Type: application/x-www-form-urlencoded[
][
]
[
][
]
test1=Hello+World&test2=This+is+a+Form+Submission&Blah=Whoop&Blah=Whoop2

Tip

This output was produced by turning on wire debugging for HttpClient, as described in Recipe 11.6.

The first line specifies the HTTP method, the request path, and the protocol. The second line identifies this client as HttpClient Version 3.0. The third line specifies the request host and is used by servers serving many different virtual hosts using one IP address. The fourth line states that the request body is exactly 72 bytes, and the Content-Type header defines the request body as being a series of URL-encoded parameters. The parameters are passed in one line as name1=value1&name2=value&, etc.

There are a few ways to specify parameters on a PostMethod. The most straightforward is to call setParameter( ) with two strings: the parameter name and parameter value. setParameter( ) will replace any existing parameter with the same name. If a parameter with the same name is already present in a PostMethod, addParameter( ) will add another value to an existing parameter; addParameter( ) also accepts two strings: the name and value. Alternatively, both methods can be called with a NameValuePair object that encapsulates the name and value of a parameter. The previous example sent two values for the parameter Blah, which were both added using addParameter( ). The first was added with two string parameters, and the second was added using a NameValuePair object.

See Also

The POST method is defined in detail in Section 9.5 of RFC 2616 (http://www.zvon.org/tmRFC/RFC2616/Output/index.html).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.190