November 2004
Intermediate to advanced
400 pages
10h 31m
English
You need to supply parameters to a script or servlet using HTTP POST.
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:
importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.HttpException;importorg.apache.commons.httpclient.NameValuePair;importorg.apache.commons.httpclient.methods.PostMethod; HttpClient client =newHttpClient( ); // Create POST method String url = "http://www.discursive.com/cgi-bin/jccook/param_list.cgi"; PostMethod method =newPostMethod( 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 ...
Read now
Unlock full access