CookieManager

java.net.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 java.net.Cookiehandler was introduced which is responsible for storing and retrieving cookies. However, there was not an actual implementation of that class. In Java 6, java.net.CookieManager was introduced which is a concrete implementation of Cookiehandler.

Tip
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();
cookiemanager.setCookiepolicy(CookiePolicy.ACCEPT_NONE); 
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.

CookieManagerExample.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;

public class CookieManagerExample {

	/** 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 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:

Domain: .google.co.in
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
Domain: .google.co.in
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 java.net.CookieManager

Download
You can download the full source code of this example here: CookieManagerExample.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.

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Igor
Igor
7 years ago

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

Tuan13
Tuan13
6 years ago

Thanks so much

hgh
hgh
4 years ago

Thanks for the example.Really helpful

Back to top button