Open In App

java.net.CookieManager Class in Java

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax:

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.

Syntax:

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.

Syntax:

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.

Syntax:

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.

Syntax:

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

Implementation:

Example

Java




// Java Program to illustrate getCookieStore()method of
// java.net.CookieManager Class
 
// java.net package is imported
// for network related things
import java.net.*;
// 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 = "https://www.geeksforgeeks.org/";
 
        // 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
        CookieHandler.setDefault(cookieManager);
 
        // 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
        cookieManager.setCookiePolicy(cookiePolicy);
 
        // 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
        connection.getContent();
 
        // 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());
        }
    }
}


Output:

The Domain is: www.geeksforgeeks.org
java.net.InMemoryCookieStore@4590c9c3


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads