CookieStore

java.net.CookieStore Example

A CookieStore is an interface that represents a storage area for cookies.CookieManager adds the cookies to the CookieStore for every HTTP response and retrieves cookies from the CookieStore for every HTTP request.

You can customize CookieStore to save cookies so that they can be used even if the Java Virtual Machine is restarted.

In this example we are going to retrieve cookies using CookieStore.
 
 
 

Project Environment

This example was implemented using the following tools :

  • Eclipse 4.3 (Kepler)
  • JDK 1.6 or greater
tip
CookieStore belongs to the package java.net and is available from jdk1.6

1. Example of CookieStore

We are going to create and open CookieStore for reading HttpCookie from specific URL. Create a java class named CookieStoreExample and paste the following code.

CookieStoreExample.java:

package com.example.javacodegeeks;

import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
/**
 * <p>The Class CookieStoreExample.</p>
 * Class is used to demonstrate example of CookieStore
 */
public class CookieStoreExample
{
   /** The Constant URL_STRING. */
   private final static String URL_STRING = "http://www.google.com";
 
   /**
    * The main method.
    *
    * @param args the arguments
    * @throws Exception the exception
   */
   public static void main(String args[]) throws Exception 
   {
 
      CookieManager cookieManager = new CookieManager();
 
      CookieHandler.setDefault(cookieManager);
 
      URL url = new URL(URL_STRING);
 
      URLConnection connection = url.openConnection();
      connection.getContent();
 
      CookieStore cookieStore = cookieManager.getCookieStore();
 
      List<HttpCookie> cookieList = cookieStore.getCookies();
 
      //iterate HttpCookie object
 
      for (HttpCookie cookie : cookieList) 
      {
         //gets domain set for the cookie
         System.out.println("Domain: "+cookie.getDomain());
 
         //gets max age of the cookie
         System.out.println("max age: "+cookie.getMaxAge());
 
         // gets name cookie
         System.out.println("name of cookie: "+cookie.getName());
 
         //gets path of the server
         System.out.println("server path: "+cookie.getPath());
 
         //gets boolean if cookie is being sent with secure protocol
         System.out.println("is cookie secure: "+cookie.getSecure());
 
         //gets the value of the cookie
         System.out.println("value of cookie: "+cookie.getValue());
 
        //gets the version of the protocol with which the given cookie is related.
        System.out.println("value of cookie: "+cookie.getVersion());
      }
   }
}

HttpCookie represents the cookie which provides the stateful session. HttpCookie is obtained from CookieStore. HttpCookie provides complete information about the cookie like max age, domain name, value path etc.
output:

Domain: .google.co.in
max age: 15811199
name of cookie: NID
server path: /
is cookie  secure: false
value of cookie: 67=X5C3qKSVM79-UOXd7Ez-y8VpgNg1Wxrpj-oQeXPBZ-vhIBmY3waYn-9VUIWunSbufMTf-n8MJ6C8HQ3PWNnFct-v-JcEPfE0YHV1gWYk2TihWaklKg1LOyXIP70l2Ovj
value of cookie: 0
Domain: .google.co.in
max age: 63071999
name of cookie: PREF
server path: /
is cookie  secure: false
value of cookie: ID=339a05aa08222b92:FF=0:TM=1422342083:LM=1422342083:S=Z7uFF14d9SdUlQyz
value of cookie: 0

Download the source code

This was an example of CookieStore in Java.

Download
You can download the full source code of this example here: CookieStoreExample.zip

Vaibhav Kulkarni

Vaibahv is PH.D candidate and holds masters degree in computer application. He is active contributor to Apache Software Foundation as well as openJDK.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button