Apache HttpClient - Cookies Management


Advertisements

Cookies are text files stored on the client computer and they are kept for various information tracking purpose.

HttpClient provides support for cookies you can create and manage cookies.

Creating a cookie

Follow the steps given below to create a cookie using HttpClient library.

Step 1 - Create Cookiestore object

The CookieStore interface represents the abstract store for Cookie objects. You can create a cookie store by instantiating the BasicCookieStore class, a default implementation of this interface.

//Creating the CookieStore object
CookieStore cookieStore = new BasicCookieStore();

Step 2 - Create ClientCookie object

In addition to the functionalities of a cookie, ClientCookie can get the original cookies in the server. You can create a client cookie by instantiating the BasicClientCookie class. To the constructor of this class, you need to pass the key-value pair that you desired to store in that particular cookie.

//Creating client cookie
BasicClientCookie clientCookie = new BasicClientCookie("name","Raju");

Step 3 - Set values to the cookie

To a client cookie, you can set/remove path, value, version, expiry date, domain, comment, and attribute using the respective methods.

Calendar myCal = new GregorianCalendar(2018, 9, 26);
Date expiryDate = myCal.getTime();
clientcookie.setExpiryDate(expiryDate);
clientcookie.setPath("/");
clientcookie.setSecure(true);
clientcookie.setValue("25");
clientcookie.setVersion(5);

Step 4 - Add cookie to the cookie store

You can add cookies to the cookie store using the addCookie() method of the BasicCookieStore class.

Add the required cookies to the Cookiestore.

//Adding the created cookies to cookie store
cookiestore.addCookie(clientcookie);

Example

Following example demonstrates how to create cookies and add them to a cookie store. Here, we created a cookie store, a bunch of cookies by setting the domain and path values, and added these to the cookie store.

import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;

public class CookieHandlingExample {
   
   public static void main(String args[]) throws Exception{
      
      //Creating the CookieStore object
      CookieStore cookiestore = new BasicCookieStore();
 
      //Creating client cookies
      BasicClientCookie clientcookie1 = new BasicClientCookie("name","Raju");
      BasicClientCookie clientcookie2 = new BasicClientCookie("age","28");
      BasicClientCookie clientcookie3 = new BasicClientCookie("place","Hyderabad");

      //Setting domains and paths to the created cookies
      clientcookie1.setDomain(".sample.com");
      clientcookie2.setDomain(".sample.com");
      clientcookie3.setDomain(".sample.com");

      clientcookie1.setPath("/");
      clientcookie2.setPath("/");
      clientcookie3.setPath("/");
 
      //Adding the created cookies to cookie store
      cookiestore.addCookie(clientcookie1);
      cookiestore.addCookie(clientcookie2);
      cookiestore.addCookie(clientcookie3);
   }
}

Retrieving a cookie

You can get the cookies added to a cookie store using getCookies() method of the asicCookieStore class. This method returns a list which holds all the cookies in the cookie store.

You can print the contents of a cookie store using the Iterator as shown below −

//Retrieving the cookies
List list = cookieStore.getCookies();

//Creating an iterator to the obtained list
Iterator it = list.iterator();
while(it.hasNext()) {
   System.out.println(it.next());
}

Example

Following example demonstrates how to retrieve cookies from a cookie store. Here, we are adding a bunch of cookies to a cookie store and retrieving them back.

import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;

public class CookieHandlingExample {
 
   public static void main(String args[]) throws Exception{
      
      //Creating the CookieStore object
      CookieStore cookiestore = new BasicCookieStore();
      
      //Creating client cookies
      BasicClientCookie clientcookie1 = new BasicClientCookie("name","Raju");
      BasicClientCookie clientcookie2 = new BasicClientCookie("age","28");
      BasicClientCookie clientcookie3 = new BasicClientCookie("place","Hyderabad");

      //Setting domains and paths to the created cookies
      clientcookie1.setDomain(".sample.com");
      clientcookie2.setDomain(".sample.com");
      clientcookie3.setDomain(".sample.com");

      clientcookie1.setPath("/");
      clientcookie2.setPath("/");
      clientcookie3.setPath("/");
 
      //Adding the created cookies to cookie store
      cookiestore.addCookie(clientcookie1);
      cookiestore.addCookie(clientcookie2);
      cookiestore.addCookie(clientcookie3);
   }
}

Output

On executing, this program generates the following output −

[version: 0][name: age][value: 28][domain: .sample.com][path: /][expiry: null]
[version: 0][name: name][value: Raju][domain: my.example.com][path: /][expiry:
null]
[version: 0][name: place][value: Hyderabad][domain: .sample.com][path:
/][expiry: null]
Advertisements