Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

11.1. Obtaining HttpClient

11.1.1. Problem

You need to use Apache HttpClient to write a program to interact with a server over HTTP.

11.1.2. Solution

To use Commons HttpClient 3.1 in a Maven 2 project, add the following dependency to your project's pom.xml:

Example 11.1. Adding a Dependency on Commons HttpClient

    <dependency>
      <groupId>commons-httpclient</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.1</version>
    </dependency> 

If you are not sure what this means, I'd suggest reading Maven: The Definitive Guide. When you depend on a library in Maven 2, all you need to do is add the dependency groupId, artifactId, and version to your project's dependencies. Once you do this, Maven 2 will download the dependency and make it available on your project's classpath.

11.1.3. Discussion

Apache HttpClient is a client library for the HTTP protocol. HttpClient is very feature-rich, supporting all HTTP methods defined in RFC 2616 (Hypertext Transfer Protocol, HTTP/1.1). Apache HttpClient supports GET, POST, PUT, OPTIONS, HEAD, and DELETE using the command pattern; HttpMethod objects are instantiated and executed by an HttpClient object that manages server interaction and state across multiple requests. HttpClient has support for various authentication mechanisms, including Basic, Digest, and NTLM authentication. HttpClient supports both HTTP and HTTPS, and the library can manage the complexity involved in using both schemes with an HTTP proxy. HttpClient can manage the state of a session, automatically keeping track of cookies across multiple requests.

11.1.4. See Also

For more information about the HttpClient 3.1 project, see the project page (http://hc.apache.org/httpclient-3.x/).


Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.