tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Tools and Libs > Commons Http Client > How to make HTTP HEAD Request

How to make HTTP HEAD Request 

Commons Http Client is a HTTP agent implementation in java supporting client-side authentication, HTTP state management and HTTP connection management. This requires the libraries httpclient-4.1.2.jar, httpcore-4.1.2.jar, httpmime-4.1.2.jar, httpclient-cache-4.1.2.jar, commons-codec.jar and commons-logging-1.1.1.jar to be in classpath. The following example shows how to make Http Head request through Http Client.

File Name  :  
com/bethecoder/tutorials/commons_httpclient/HeadRequestTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.commons_httpclient;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * The HEAD method is identical to GET except that the server MUST NOT 
 * return a message-body in the response. The meta-information contained 
 * in the HTTP headers in response to a HEAD request SHOULD be identical to 
 * the information sent in response to a GET request. This method can be 
 * used for obtaining meta-information about the entity implied by the 
 * request without transferring the entity-body itself. 
 
 * This method is often used for testing hypertext links for validity, 
 * accessibility, and recent modification.
 
 * - Source www.w3.org
 */
public class HeadRequestTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpHead httphead = new HttpHead("http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG");
    System.out.println("Requesting : " + httphead.getURI());

    try {
      
      HttpResponse response = httpclient.execute(httphead);
      System.out.println("Protocol : " + response.getProtocolVersion());
            System.out.println("Status code : " + response.getStatusLine().getStatusCode());
            System.out.println("Status line : " + response.getStatusLine());
      
            Header [] headers = response.getAllHeaders();
            for (Header header : headers) {
              System.out.println(" --> " + header.getName() ":" + header.getValue());
            }
            
            System.out.println("\n\nResponse : ");
      if (response.getEntity() != null) {
        System.out.println(EntityUtils.toString(response.getEntity()));
      else {
        System.out.println("As expected no response body for HEAD request");
      }
      
    catch (ClientProtocolException e) {
      e.printStackTrace();
    catch (IOException e) {
      e.printStackTrace();
    finally {
       httpclient.getConnectionManager().shutdown();
    }
  }

}
   

It gives the following output,
Requesting : http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOG

Protocol : HTTP/1.1
Status code : 200
Status line : HTTP/1.1 200 OK

 --> Content-Type:text/html; charset=ISO-8859-1
 --> Set-Cookie:SC=RV=:ED=us; expires=Sun, 17-Jan-2038 19:14:07 GMT; 
 		path=/finance; domain=.google.com
 --> Set-Cookie:PREF=ID=218c92556274d542:TM=1331294978:LM=1331294978:
 		S=7GfUfW2SNc5e8a2m; expires=Sun, 09-Mar-2014 12:09:38 GMT; 
 		path=/; domain=.google.com
 --> Date:Fri, 09 Mar 2012 12:09:38 GMT
 --> Pragma:no-cache
 --> Expires:Fri, 01 Jan 1990 00:00:00 GMT
 --> Cache-Control:no-cache, no-store, must-revalidate, proxy-revalidate
 --> X-UA-Compatible:IE=EmulateIE7
 --> X-Content-Type-Options:nosniff
 --> Content-Length:197
 --> X-Frame-Options:SAMEORIGIN
 --> X-XSS-Protection:1; mode=block
 --> Server:GSE


Response : 
As expected no response body for HEAD request


                    Print

   



Books Recommendation

 
 
bl  br