Open In App Class in Java

Last Updated : 23 Jan, 2023
Like Article

The CookieManager class provides a precise implementation of CookieHandler. This separates the storage of cookies from the policy surrounding accepting and rejecting cookies. A CookieManager is initialized with a CookieStore and a CookiePolicy. The CookieStore manages storage, and the CookiePolicy object makes policy decisions on cookie acceptance/rejection.


public CookieManager()
// Create a new cookie manager
public CookieManager(CookieStore store, CookiePolicy cookiePolicy)
// Create a new cookie manager 
// with specified cookie store and cookie policy

The methods inside the CookieManager class is shown below in the table 

Method Action Performed 
 getCookieStore() This method will retrieve the current cookie store.
setCookiePolicy(CookiePolicy cookiePolicy) This method will set the cookie policy of this cookie manager.
get(URI uri, Map<String, List<String>> requestHeaders) This method will get all the applicable cookies from a cookie cache for the specified URI in the request header.
put(URI uri, Map<String, List<String>> responseHeaders) This method will set all the applicable cookies

Let us discuss all four methods of this class individually to get a better understanding. Here we go:

Method 1: 

getCookieStore() method retrieves the current cookie store and returns the cookie store currently used by the cookie manager.


public CookieStore getCookieStore() ;

Method 2: 

setCookiePolicy() method sets the cookie policy of the cookie manager. An instance of CookieManager will have cookie policy ACCEPT_ORIGINAL_SERVER by default. This method can be called to set another cookie policy.


public void setCookiePolicy(CookiePolicy cookiePolicy) ;

Method 3: 

get() method gets all the applicable cookies from a cookie cache for the required URI within the request header. It is up to the implementation to require under consideration the URI and therefore the cookies attributes and security settings to work out which of them should be returned. HTTP’s protocol implementers should confirm that this method is called after all request headers associated with choosing cookies are added and before the request is sent.


public Map<String,List<String>> get(URI uri, Map<String,List<String>> requestHeaders)

Parameters: The URI passed as an argument specifies the intended use for the cookies. In particular, the scheme should reflect whether the cookies are going to be sent over HTTP, HTTPS, or utilized in another context like JavaScript.

Method 4: 

put() method sets all the applicable cookies, for example, response header fields that are named Set-Cookie2, present in the response headers into a cookie cache.


public void put(URI uri, Map<String,List<String>> responseHeaders)




// Java Program to illustrate getCookieStore()method of
// Class
// package is imported
// for network related things
// Importing List class from
// java.util package
import java.util.List;
// Class
// To set CookiePolicy in JavaCookieManager Class
public class GFG {
    // Main driver method
    public static void main(String args[]) throws Exception
        // Random URI as input
        String uri = "";
        // Creating an object of Cookie Manager Class
        // Creating an object of CookieManager Class
        CookieManager cookieManager = new CookieManager();
        //  Setting and unsetting system wide Cookie Handler
        // using the setDefault() method
        // Pre-defined policy that accepts cookies
        // from original server.
        CookiePolicy cookiePolicy
            = CookiePolicy.ACCEPT_ORIGINAL_SERVER;
        // Setting the cookie policy of the cookieManager
        // class using the setCookiePolicy() method
        // Now, creating an object of URL class
        URL url = new URL(uri);
        // Establishing the connection over the object
        // of URLConnection class
        URLConnection connection = url.openConnection();
        // Receiving the response
        // Lastly, creating object of CookieStore to
        // retrieve current cookie store instance
        CookieStore cookieStore
            = cookieManager.getCookieStore();
        // For this, creating an List of object type
        // HttpCookie
        List<HttpCookie> cookieList
            = cookieStore.getCookies();
        // Iterating over the List of objects
        // using the for each loop
        for (HttpCookie cookie : cookieList) {
            // Print the Domain name and Cookie name using
            // using getName() and getDomain() method
            System.out.println("Domain name is: "
                               + cookie.getDomain());
            System.out.println("Cookie name is: "
                               + cookie.getName());


The Domain is:

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads