java.net.CookieManager Class in Java
Last Updated :
23 Jan, 2023
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
import java.net.*;
import java.util.List;
public class GFG {
public static void main(String args[]) throws Exception
{
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
CookiePolicy cookiePolicy
= CookiePolicy.ACCEPT_ORIGINAL_SERVER;
cookieManager.setCookiePolicy(cookiePolicy);
URL url = new URL(uri);
URLConnection connection = url.openConnection();
connection.getContent();
CookieStore cookieStore
= cookieManager.getCookieStore();
List<HttpCookie> cookieList
= cookieStore.getCookies();
for (HttpCookie cookie : cookieList) {
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
Share your thoughts in the comments
Please Login to comment...