📜  Java中的Java .net.CookiePolicy 类

📅  最后修改于: 2022-05-13 01:55:37.808000             🧑  作者: Mango

Java中的Java .net.CookiePolicy 类

CookiePolicy实现决定哪些 cookie 应该被接受,哪些应该被拒绝。提供了三种预定义的策略实现,即 ACCEPT_ALL、ACCEPT_NONE 和 ACCEPT_ORIGINAL_SERVER。

签名

public interface CookiePolicy

字段

S.NO      

Field                               

Description                                                                                          

Data Type                                          

1.ACCEPT_ALL                    CookiePolicy.ACCEPT_ALL is a predefined policy that accepts all the cookies.static CookiePolicy
2.ACCEPT_ORIGINAL_SERVER                    CookiePolicy.ACCEPT_ORIGINAL_SERVER is a predefined policy that only accepts cookies from the original server.static CookiePolicy
3.ACCEPT_NONECookiePolicy.ACCEPT_NONE is a predefined policy that accepts no cookies.static CookiePolicy

方法

CookiePolicy 接口仅包含一个名为shouldAccept(URI uri, HttpCookie cookie)的方法。

shouldAccept(URI uri , HttpCookie cookie) 方法

句法:

boolean shouldAccept(URI uri, HttpCookie cookie).

方法参数:

shouldAccept() 有两个参数,分别是 URI 和 HttpCookie 类型

方法返回类型:

shouldAccept() 具有布尔返回类型,如果应该接受 cookie,则返回 true,否则返回 false。

定义我们自己的 Cookie 政策

定义 cookie 策略 –

  • 实现 CookiePolicy 接口。
  • 定义CookiePolicy的shouldAccept方法,根据需要进行编程。

例子

1. Java程序设置 cookieManager cookie 策略以接受所有 cookie –

Java
import java.net.*;
class GFG {
    public static void main(String[] args)
    {
        // create instance of cookieManager
        CookieManager cookieManager = new CookieManager();
        // set cookieManager cookie policy using
        // setCookiePolicy method
        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        System.out.println("Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_ALL");
    }
}


Java
import java.net.*;
class GFG {
    public static void main(String[] args)
    {
        // create instance of cookieManager
        CookieManager cookieManager = new CookieManager();
        // set cookieManager cookie policy using
        // setCookiePolicy method
        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_NONE);
        System.out.println("Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_NONE");
    }
}


Java
import java.net.*;
class GFG {
    public static void main(String[] args)
    {
        // create instance of cookieManager
        CookieManager cookieManager = new CookieManager();
        // set cookieManager cookie policy using
        // setCookiePolicy method
        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
        System.out.println("Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_ORIGINAL_SERVER");
    }
}


输出
Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_ALL

2. Java程序设置 cookieManager cookie 策略以不接受 cookie –

Java

import java.net.*;
class GFG {
    public static void main(String[] args)
    {
        // create instance of cookieManager
        CookieManager cookieManager = new CookieManager();
        // set cookieManager cookie policy using
        // setCookiePolicy method
        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_NONE);
        System.out.println("Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_NONE");
    }
}
输出
Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_NONE

3. Java程序设置 cookieManager cookie 策略以仅接受来自原始服务器的 cookie –

Java

import java.net.*;
class GFG {
    public static void main(String[] args)
    {
        // create instance of cookieManager
        CookieManager cookieManager = new CookieManager();
        // set cookieManager cookie policy using
        // setCookiePolicy method
        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
        System.out.println("Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_ORIGINAL_SERVER");
    }
}
输出
Cookie Policy for cookieManager is set to : CookiePolicy.ACCEPT_ORIGINAL_SERVER