CookieManager Example

A Cookie is a small piece of data (such as browsing activity) sent from a website and stored in a user’s web browser while the user is browsing that website.

In Java 5, an abstract class was introduced which is responsible for storing and retrieving cookies. However, there was not an actual implementation of that class. In Java 6, was introduced which is a concrete implementation of Cookiehandler.

By default CookieManager is disabled.

To store or return cookies, you need to enable it:

CookieManagercookiemanager = new CookieManager();
cookiehandler.setDefault(cookiemanager );

Only two line of code ares required if you want to receive cookies from a site or send them back. In case you want to be more careful about cookies there are predefined policies which you can use:

  • CookiePolicy.ACCEPT_ALL all cookies are allowed
  • CookiePolicy.ACCEPT_NONE no cookies are allowed
  • CookiePolicy.ACCEPT_ORIGNAL_SERVER only first party cookies are allowed

For example we don’t want to accept any cookies from server:

CookieManager cookiemanager = new CookieManager();
cookiehandler.setDefault(cookiemanager );

1. An Example

In this example we will use CookieManager to accept all cookies by default. Create a java class named CookieManagerExample and paste the following code.

package com.example.javacodegeeks;

import java.util.List;

public class CookieManagerExample {

	/** The Constant URL_STRING. */

	private final static String URL_STRING = "";

	 * The main method.
	 * @param args the arguments
	 * @throws Exception the exception
	public static void main(String args[]) throws Exception 
		CookieManager cookieManager = new CookieManager();
		URL url = new URL(URL_STRING);
		URLConnection connection = url.openConnection();
		CookieStore cookieStore = cookieManager.getCookieStore();
		List 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());



1.1 Output:

max age: 15811199
name of cookie: NID
server path: /
is cookie secure: false
value of cookie: 67=b8and-4WovKO1UZD69r1iNjUSq76dzOVVQFCVjSyuciYofiOrMDMEIwu-QGy-M_ScndR_5iGbG5uP4LLwR33bDKWZ6XXgkIRC9cn5hQiw96vaKBHlLJlVa0g8LVj39ds
value of cookie: 0
max age: 63071999
name of cookie: PREF
server path: /
is cookie secure: false
value of cookie: ID=9c67bcf786ea8a51:FF=0:TM=1423491949:LM=1423491949:S=ZFSUR_dP7dGDqlSC
value of cookie: 0

2 Download the source code

This was an example of

You can download the full source code of this example here:

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.
Notify of

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

Newest Most Voted
Inline Feedbacks
View all comments
6 years ago

Great example, the one I was looking for. Thanks a lot.
A small typo – on line 37 there should be List

6 years ago

Thanks so much

4 years ago

Thanks for the example.Really helpful

Back to top button