get(URI, Map): This method is available in the java.net.CookieManager class of Java.
Syntax:
Map<String, List<String>> java.net.CookieManager.get(URI uri, Map<String, List<String>> requestHeaders) throws IOException
This method takes two arguments. This method gets all the applicable cookies from a cookie cache for the specified uri in the request header.
The URI passed as an argument specifies the intended use for the cookies.
In particular, the scheme should reflect whether the cookies will be sent over http, https, or used in another context like javascript. The host part should reflect either the destination of the cookies or their origin in the case of javascript.
It is up to the implementation to take into account the URI and the cookie's attributes and security settings to determine which ones should be returned.
HTTP protocol implementers should make sure that this method is called after all request headers related to choosing cookies are added, and before the request is sent.
Parameters: Two parameters are required for this method.
uri: a URI representing the intended use for the cookies.
requestHeaders: - a Map from request header field names to lists of field values representing the current request headers.
Returns: an immutable map from state management headers, with field names "Cookie" or "Cookie2" to a list of cookies containing state information.
Throws:
1. IOException - if an I/O error occurs
Approach
Java
package com.CookieManager;import java.io.IOException;import java.net.CookieManager;import java.net.URI;import java.net.URISyntaxException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class CookieManagerget {public static void main(String[] args)throws IOException, URISyntaxException {CookieManager cookieManager = new CookieManager();URI uri = new URI("/hello");Map<String, List<String>> map = new HashMap<>();List<String> value = new ArrayList<>();value.add("java");map.put("hello", value);System.out.println(cookieManager.get(uri, map));}}
Output:
{Cookie=[]}
Some other methods of CookieManager
CookieManager(): Create a new cookie manager.
CookieManager(CookieStore, CookiePolicy): This method creates a new cookie manager with a specified cookie store and cookie policy.
get(URI, Map): This method gets all the applicable cookies from a cookie cache for the specified uri in the request header.
getCookieStore(): To retrieve current cookie store.
put(URI, Map): This method sets all the applicable cookies, examples are response header fields that are named Set-Cookie2, present in the response headers into a cookie cache.
setCookiePolicy(CookiePolicy): To set the cookie policy of this cookie manager.
No comments:
Post a Comment