Tuesday, May 31, 2011

Concrete HttpClient Example

Apache/Jakarta is one of the major reasons Java development has been a mainstay for as long as it has. The Organization has created some of the most useful tools and frameworks available for Java. One tool I came across 5 years ago was the HttpClient package. The project seems to have undergone many changes and added a lot stuff. I downloaded version 4.1 of the package the other day and had a difficult time reconciling my old idea of HttpClient and the current offering. The documentation is strangely lacking in concrete  examples. Here's a chunk of code to get you going with basic authentication in HttpClient 4.1



   String host = "somehost.com";
   String fullUrl = "somehost.com/things/sub";
   String username = "username";
   String password = "password";

   HttpHost targetHost = new HttpHost(host, 80);
   DefaultHttpClient client = new DefaultHttpClient();
   AuthScope authScope = new AuthScope(targetHost.getHostName(),targetHost.getPort());
   UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
   client.getCredentialsProvider().setCredentials(authScope, creds);

   try {
      HttpGet simpleRequest = new HttpGet(fullUrl);
      ResponseHandler responseHandler = new BasicResponseHandler();
      String serverResponse = client.execute(
simpleRequest , responseHandler);
      System.out.println(serverResponse);
   } catch (Exception e) {
       System.out.println("Exception while retrieving data : " + e);
   } finally {
       client.getConnectionManager().shutdown();
    }





That's it, if you don't need authentication you can just not mess with the authscope and credentials stuff. The package seems to be powerful enough build a webserver out of, but sometimes you just need to scrape a website :)

No comments:

Post a Comment