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
.
By default
CookieManager
is disabled.To store or return cookies, you need to enable it:
1 2 | 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 allowedCookiePolicy.ACCEPT_NONE
no cookies are allowedCookiePolicy.ACCEPT_ORIGNAL_SERVER
only first party cookies are allowed
For example we don’t want to accept any cookies from server:
1 2 3 | 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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 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. */ /** * 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:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | 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
You can download the full source code of this example here: CookieManagerExample.zip
Great example, the one I was looking for. Thanks a lot.
A small typo – on line 37 there should be List
Thanks so much
Thanks for the example.Really helpful