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:
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.util.URIUtil; HttpClient client = new HttpClient( ); String url = "http://www.discursive.com/cgi-bin/jccook/param_list.cgi"; HttpMethod method = new GetMethod( 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
In the previous example, method.setQueryString()
is used to set the entire query string at once, but there
is another alternative: setting the query string with an array of
NameValuePair
objects. When a
NameValuePair[]
is passed to method.setQueryString( )
, the HttpMethod
then takes each pair and creates a
series of parameters delimited by an ampersand. The approach encourages
cleaner code because you are not concatenating strings to pass multiple
parameters. The following example sets the same parameters used in the
previous example, using NameValuePair
objects:
// Set query string with name value pair objects HttpMethod method = new GetMethod( url ); NameValuePair pair = new NameValuePair( "test1", URIUtil.encodeQuery( "O Reilly" ) ); NameValuePair pair2 = new NameValuePair( "blah", URIUtil.encodeQuery( "Whoop" ) ); NameValuePair[] pairs = new NameValuePair[] { pair, pair2 }; method.setQueryString( pairs ); System.out.println( "With NameValuePairs: " + method.getURI( ) ); client.executeMethod( method ); System.out.println( "Response:\n " + method.getResponseBodyAsString( ) ); method.releaseConnection( );
According to RFC 1738 (Uniform Resource Locators (URL)
specification) URLs can only contain alphanumeric characters, [0-9a-zA-Z]
, and a few other special
characters. If you need to send a parameter with an unacceptable
character in a URL, you will need to encode your string according to the
standard defined in RFC 1738. URIUtil
exposes a method encodeQuery( )
that
can be used to encode the value "O Reilly" in the previous example. The
following code demonstrates the use of URIUtil
to encode strings for inclusion in a
URL:
String encoded1 = URIUtil.encodeQuery( "<test>=O'Connell" ); String encoded2 = URIUtil.encodeQuery( "one:two=thr ee#" ); String decoded = URIUtil.decode( "Hello%20World%3F" ); System.out.println( "Encoded: " + encoded1 ); System.out.println( "Encoded: " + encoded2 ); System.out.println( "Decoded: " + decoded );
This simple example encodes two strings and decodes an encoded
string using URIUtil
. The output
shows the result of each transformation:
Encoded: %3ctest%e3=O'Connell Encoded: one%3atwo=thr%20ee#23 Decoded: Hello World?
In this example, URLUtil
was
used to encode content passed in on the query string. This HttpClient
team has recently moved some of the URL encoding and decoding logic to
the Commons Codec project as a class named URLCodec
. For more information about URLCodec
, see the Commons Codec project page
(http://commons.apache.org/codec).
RFC 1738 discusses the legal characters for a URL and defines a process for encoding other characters. RFC 1738 can be found at http://www.zvon.org/tmRFC/RFC2616/Output/index.html.