|
It goes without saying that there is a staggering amount of data available on the Internetspecifically, available via the World Wide Web (HTTP). One of the wonderful things about the JDK is the built-in ability to access resources via HTTPspecifically, the java.net.HttpURLConnection class shows that simple HTTP resources can easily be downloaded by Java applications. Unfortunately, the built-in HTTP connectivity in the JDK suffers from some significant limitations (for a partial feature set comparison, see http://www.nogoop.com/product_16.html#compare). Fortunately, these limitations are overcome when using the Commons HttpClient. A full list of features supported by HttpClient (including links to the relevant RFCs) can be found at http://jakarta.apache.org/commons/httpclient/features.html, but some of the most interesting features include:
Glancing at Figure 3-1, which shows the non-Exception classes just in the org.apache.commons.httpclient package, you might think that the HttpClient package is complex and difficult to use. While exercising the full range of capability may be complex, we'll look at how we can use the HttpClient classes to easily deal with cookies and posting forms. Figure 3-1. Main HttpClient classes.The biggest difference between HttpClient and the default JDK HTTP connectivity is the use of classes to implement each of the various HTTP requests. These classes are then passed to a persistent HTTP client object to execute. Listing 3-1 shows a simple GET request. The response to a particular method invocation is bound to the method object, not the client. Listing 3-1. Simple GET RequestHttpClient client = new HttpClient(); HttpMethod method = new GetMethod("http://www.cascadetg.com/"); client.executeMethod(method); System.out.println(method.getStatusCode()); System.out.println(method.getResponseBodyAsString()); method.releaseConnection();
|
|