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 read Response Headers

How to read Response Headers 

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 read response headers through Commons Http Client.

File Name  :  
com/bethecoder/tutorials/commons_httpclient/ResponseHeadersTest.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.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class ResponseHeadersTest {

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

    try {
      
            HttpResponse response = httpclient.execute(httpget);
            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()));
      }
      
    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

--> 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=18919f25f97a585e:TM=1331320873:LM=1331320873:
		S=7NW57HB3UeyMWdA8; expires=Sun, 09-Mar-2014 19:21:13 GMT; 
		path=/; domain=.google.com
--> Date:Fri, 09 Mar 2012 19:21:13 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
--> X-Frame-Options:SAMEORIGIN
--> X-XSS-Protection:1; mode=block
--> Server:GSE
--> Transfer-Encoding:chunked


Response : 

// [
{
"id": "694653"
,"t" : "GOOG"
,"e" : "NASDAQ"
,"l" : "603.80"
,"l_cur" : "603.80"
,"s": "0"
,"ltt":"2:21PM EST"
,"lt" : "Mar 9, 2:21PM EST"
,"c" : "-3.34"
,"cp" : "-0.55"
,"ccol" : "chr"
}
]


                    Print

   



Books Recommendation

 
 
bl  br