Java的.net.CookieManager类在Java中
CookieManager 类提供了 CookieHandler 的精确实现。这将 cookie 的存储与接受和拒绝 cookie 的政策分开。 CookieManager 使用 CookieStore 和 CookiePolicy 进行初始化。 CookieStore 管理存储,而 CookiePolicy 对象对 cookie 接受/拒绝做出政策决定。
句法:
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
CookieManager 类内部的方法如下表所示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 This method will get all the applicable cookies from a cookie cache for the specified URI in the request header. put(URI uri, Map This method will set all the applicable cookies
让我们分别讨论这个类的所有四种方法,以获得更好的理解。开始了:
方法一:
getCookieStore() 方法检索当前 cookie 存储并返回 cookie 管理器当前使用的 cookie 存储。
句法:
public CookieStore getCookieStore() ;
方法二:
setCookiePolicy() 方法设置 cookie 管理器的 cookie 策略。默认情况下,CookieManager 的实例将具有 cookie 策略 ACCEPT_ORIGINAL_SERVER。可以调用此方法来设置另一个 cookie 策略。
句法:
public void setCookiePolicy(CookiePolicy cookiePolicy) ;
方法三:
get() 方法从请求标头中所需 URI 的 cookie 缓存中获取所有适用的 cookie。实现需要考虑 URI,因此需要考虑 cookie 属性和安全设置,以确定应该返回哪些属性。 HTTP 的协议实现者应确认在添加与选择 cookie 相关的所有请求标头之后和发送请求之前调用此方法。
句法:
public Map> get(URI uri, Map> requestHeaders)
参数:作为参数传递的 URI 指定了 cookie 的预期用途。特别是,该方案应反映 cookie 是通过 HTTP、HTTPS 发送还是在其他上下文(如 JavaScript)中使用。
方法四:
放() 方法将所有适用的 cookie,例如,响应头中存在的名为 Set-Cookie2 的响应头字段设置到 cookie 缓存中。
句法:
public void put(URI uri, Map> responseHeaders)
执行:
例子
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 pcckage
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 cookieList
= cookieStore.getCookies();
// Iterating ove 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: www.geeksforgeeks.org
java.net.InMemoryCookieStore@4590c9c3