Create an instance of HttpClient
and use it to execute a GetMethod
object. Once the method has been executed, the response
body can be accessed as an InputStream
, byte[]
, or String
. The following example gets the
contents of http://www.discursive.com/jccook/
and retrieves the response body as a string:
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.methods.GetMethod; HttpClient client = new HttpClient( ); String url = "http://www.discursive.com/jccook/"; HttpMethod method = new GetMethod( url ); try { client.executeMethod( method ); if( method.getStatusCode( ) == HttpStatus.SC_OK ) { String response = method.getResponseBodyAsString( ); System.out.println( response ); } } catch( HttpException he ) { System.out.println( "HTTP Problem: " + he.getMessage( ) ); } catch( IOException ioe ) { System.out.println( "IO Exeception: " + ioe.getMessage( ) ); } finally { method.releaseConnection( ); method.recycle( ); }
This code retrieves the content of http://www.discursive.com/jccook
using the HTTP GET method. If the response code is HttpStatus.SC_OK
or 200
, the response is printed to the
console:
<html> <head> <title>JCCook Example</title> </head> <body> <h1>Hello World!</h1> </body> </html>
Note the exception handling involved in this example. Performing a
simple HTTP GET called for two catch blocks: HttpException
and IOException
. An HttpException
is thrown if there is a problem
relating to the HTTP protocol, and an IOException
is thrown if there is a problem
with the network. Examples in this chapter omit the rigorous exception
handling from the previous example; you can assume that every call to
execute()
is surrounded by the
appropriate try
/catch
block.
GetMethod
is an implementation
of the HttpMethod
interface, which is
executed by HttpClient
. The lifecycle
of any HttpMethod
implementation is
straightforward; an HttpMethod
is
created, executed by an instance of HttpClient
, and, once the response has been
examined, the connection is released and the method is recycled. When an
HttpMethod
object is recycled by a
call to recycle( )
, it is a signal to
the system that this specific HttpMethod
instance can be used again.
releaseConnection( )
instructs
HttpClient
to release the connection
that is associated with an HttpMethod
instance. No matter what happens during the execution of a method, the
releaseConnection( )
must be called
to free network resources.
Once a method has been executed, you can get the response status
code from method.getStatusCode( )
.
This method returns an int
, which
will correspond to one of the public static
final
variables on HttpStatus
. Some of the more common status
codes on HttpStatus
are SC_OK
(200), SC_NOT_FOUND
(404), SC_INTERNAL_SERVER_ERROR
(500), SC_MOVED_TEMPORARILY
(302), and SC_UNAUTHORIZED
(401). For a full list of HTTP
status codes, see the Javadoc for HttpStatus
. When a server sends back a bad
HTTP status, it is sometimes accompanied by a short message. This
message can be read by calling method.getStatusText( )
.
For a formal definition of the HTTP GET method, see Section 9.3 of RFC 2616 at http://www.zvon.org/tmRFC/RFC2616/Output/index.html.
For a full list of HTTP status codes, see the HttpStatus
Javadoc at http://hc.apache.org/httpclient-3.x/apidocs/index.html.