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 Cookies

How to read Cookies 

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 cookies through Http Client.

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

import java.io.IOException;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class GetCookiesTest {

  /**
   @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 {
      //Create a local instance of cookie store
            CookieStore cookieStore = new BasicCookieStore();

            //Create local HTTP context
            HttpContext localContext = new BasicHttpContext();
            
            //Bind custom cookie store to the local context
            localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
            
            //Get Response
            HttpResponse response = httpclient.execute(httpget, localContext);

            //Get Headers
            Header [] headers = response.getAllHeaders();
            for (Header header : headers) {
              System.out.println(" --> " + header.getName() ":" + header.getValue());
            }
            
            //Get Cookies
            System.out.println("\n\nCookies : ");
            List<Cookie> cookies = cookieStore.getCookies();
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("Cookie: " + cookies.get(i));
            }
            
      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=4a00f84069712d54:TM=1331293427:LM=1331293427:
 	S=xOuVyHiWrlwU-huX;expires=Sun, 09-Mar-2014 11:43:47 GMT; 
 	path=/; domain=.google.com
 --> Date:Fri, 09 Mar 2012 11:43:47 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


Cookies : 
Cookie: [version: 0][name: PREF][value: ID=4a00f84069712d54:TM=1331293427:
	LM=1331293427:S=xOuVyHiWrlwU-huX][domain: .google.com][path: /]
	[expiry: Sun Mar 09 17:13:47 IST 2014]
Cookie: [version: 0][name: SC][value: RV=:ED=us][domain: .google.com]
	[path: /finance][expiry: Mon Jan 18 00:44:07 IST 2038]


Response : 

// [
{
"id": "694653"
,"t" : "GOOG"
,"e" : "NASDAQ"
,"l" : "607.14"
,"l_cur" : "607.14"
,"s": "0"
,"ltt":"4:00PM EST"
,"lt" : "Mar 8, 4:00PM EST"
,"c" : "+0.34"
,"cp" : "0.06"
,"ccol" : "chg"
}
]


                    Print

   



Books Recommendation

 
 
bl  br