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.

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.

  1. //Creating the CookieStore object
  2. 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.

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

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

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

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

Add the required cookies to the Cookiestore.

  1. //Adding the created cookies to cookie store
  2. 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.

  1. import org.apache.http.client.CookieStore;
  2. import org.apache.http.impl.client.BasicCookieStore;
  3. import org.apache.http.impl.cookie.BasicClientCookie;
  4.  
  5. public class CookieHandlingExample {
  6.  
  7. public static void main(String args[]) throws Exception{
  8.  
  9. //Creating the CookieStore object
  10. CookieStore cookiestore = new BasicCookieStore();
  11.  
  12. //Creating client cookies
  13. BasicClientCookie clientcookie1 = new BasicClientCookie("name","Raju");
  14. BasicClientCookie clientcookie2 = new BasicClientCookie("age","28");
  15. BasicClientCookie clientcookie3 = new BasicClientCookie("place","Hyderabad");
  16.  
  17. //Setting domains and paths to the created cookies
  18. clientcookie1.setDomain(".sample.com");
  19. clientcookie2.setDomain(".sample.com");
  20. clientcookie3.setDomain(".sample.com");
  21.  
  22. clientcookie1.setPath("/");
  23. clientcookie2.setPath("/");
  24. clientcookie3.setPath("/");
  25.  
  26. //Adding the created cookies to cookie store
  27. cookiestore.addCookie(clientcookie1);
  28. cookiestore.addCookie(clientcookie2);
  29. cookiestore.addCookie(clientcookie3);
  30. }
  31. }

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 −

  1. //Retrieving the cookies
  2. List list = cookieStore.getCookies();
  3.  
  4. //Creating an iterator to the obtained list
  5. Iterator it = list.iterator();
  6. while(it.hasNext()) {
  7. System.out.println(it.next());
  8. }

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.

  1. import org.apache.http.client.CookieStore;
  2. import org.apache.http.impl.client.BasicCookieStore;
  3. import org.apache.http.impl.cookie.BasicClientCookie;
  4.  
  5. public class CookieHandlingExample {
  6.  
  7. public static void main(String args[]) throws Exception{
  8.  
  9. //Creating the CookieStore object
  10. CookieStore cookiestore = new BasicCookieStore();
  11.  
  12. //Creating client cookies
  13. BasicClientCookie clientcookie1 = new BasicClientCookie("name","Raju");
  14. BasicClientCookie clientcookie2 = new BasicClientCookie("age","28");
  15. BasicClientCookie clientcookie3 = new BasicClientCookie("place","Hyderabad");
  16.  
  17. //Setting domains and paths to the created cookies
  18. clientcookie1.setDomain(".sample.com");
  19. clientcookie2.setDomain(".sample.com");
  20. clientcookie3.setDomain(".sample.com");
  21.  
  22. clientcookie1.setPath("/");
  23. clientcookie2.setPath("/");
  24. clientcookie3.setPath("/");
  25.  
  26. //Adding the created cookies to cookie store
  27. cookiestore.addCookie(clientcookie1);
  28. cookiestore.addCookie(clientcookie2);
  29. cookiestore.addCookie(clientcookie3);
  30. }
  31. }

Output

On executing, this program generates the following output −

  1. [version: 0][name: age][value: 28][domain: .sample.com][path: /][expiry: null]
  2. [version: 0][name: name][value: Raju][domain: my.example.com][path: /][expiry:
  3. null]
  4. [version: 0][name: place][value: Hyderabad][domain: .sample.com][path:
  5. /][expiry: null]