On The Insider: Michael Jackson In Wheelchair

Build Web clients with HTTP Client

Tags: Channel management, David Petersheim, Web, HttpClient, UsernamePasswordCredentials, HTTP-Client, GetMethod, Java Tips Newsletter

  • Save
  • Print
  • Digg This
  • 2

Takeaway: David Petersheim demonstrates how using Jakarta Commons' HTTP Client to build a Web-aware application is straightforward.

The next time you're building a Web-aware application and the Java API isn't enough, you may want to check out Jakarta Commons' HTTP Client. Using HTTP Client is straightforward: Create an instance of HttpClient, create an instance of the method type you want to use, and then execute the method using the instance of HttpClient.

Below is an example of fetching a Web page and dumping its content to standard out.

    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod("http://www.google.com/");

    client.executeMethod(get);
    System.out.println(get.getResponseBodyAsString());

Now suppose that you need to use the basic authentication mechanism to access a page. In this case, you need to use the HTTP Client class UsernamePasswordCredentials. Here's the code to add to accomplish this:

    UsernamePasswordCredentials upc =
            new UsernamePasswordCredentials("foo", "bar");

    client.getState().setCredentials(null, null, upc);

    get.setDoAuthentication(true);

In the following code, we add a timeout specification to the get method in case the page takes a long time to load.

    client.setConnectionTimeout(60000);
    

As the sample code illustrates, making use of the features in HTTP Client is simple. If your application needs HTTP access, then check out HTTP Client. It has more features than the Web-aware classes in the Java API, and it's easy to use. Take a look for yourself and see if it suits your needs.

import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.GetMethod;

public class HttpClientTip {
    public static void main(String args[]) {
        try {
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod("http://www.google.com/");

            UsernamePasswordCredentials upc =
                    new UsernamePasswordCredentials("foo", "bar");

            client.getState().setCredentials(null, null, upc);

            get.setDoAuthentication(true);

            client.setConnectionTimeout(60000);

            client.executeMethod(get);
            System.out.println(get.getResponseBodyAsString());
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Find out more about HTTP Client's many features, including HTTPS, multipart POSTs, and proxy support, by visiting the Apache Jakarta Project site.

Delivered each Thursday, our free Java newsletter provides insight and hands-on tips you need to unlock the full potential of this programming language. Automatically sign up today!

  • Save
  • Print
  • Digg This
  • 2

What do you think?

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET

advertisement
Click Here