November 2004
Intermediate to advanced
400 pages
10h 31m
English
You need to send query parameters in a URL.
Set the query string using the setQueryString( )
method on an instance of HttpMethod. Use
URIUtil to encode any text included in a URL. The
following example puts two parameters on the query string:
importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.HttpException;importorg.apache.commons.httpclient.HttpMethod;importorg.apache.commons.httpclient.NameValuePair;importorg.apache.commons.httpclient.methods.GetMethod;importorg.apache.commons.httpclient.util.URIUtil; HttpClient client =newHttpClient( ); String url = "http://www.discursive.com/cgi-bin/jccook/param_list.cgi"; HttpMethod method =newGetMethod( url ); // Set the Query String with setQueryString( )method.setQueryString(URIUtil.encodeQuery("test1=O Reilly&blah=Whoop"));System.out.println( "With Query String: " + method.getURI( ) ); client.executeMethod( method ); System.out.println( "Response:\n " + method.getResponseBodyAsString( ) ); method.releaseConnection( );
The param_list.cgi CGI script echoes all
parameters received, and from the following output, you can see how
URIUtil encodes the first parameter:
With Query String: http://www.discursive.com/cgi-bin/jccook/param_list. cgi?test1=O%20Reilly&blah=Whoop Response: These are the parameters I received: test1: O Reilly blah: Whoop
The question mark is understood, and you do not need to supply a leading question mark to the ...
Read now
Unlock full access