模块  java.base
软件包  java.net

Class CookieManager


  • public class CookieManagerextends CookieHandler
    CookieManager提供了CookieHandler的具体实现, 它将 cookie的存储与接受和拒绝cookie的策略分开。 那么CookieManager初始化为CookieStore ,其管理存储和CookiePolicy对象,这使得上的cookie接受/拒绝决策。

    java.net包中的HTTP cookie管理如下所示:

                      use CookieHandler <------- HttpURLConnection       ^       | impl       |         use CookieManager -------> CookiePolicy             |   use             |--------> HttpCookie             |              ^             |              | use             |   use        |             |--------> CookieStore                            ^                            | impl                            |                  Internal in-memory implementation 
    • CookieHandler is at the core of cookie management. User can call CookieHandler.setDefault to set a concrete CookieHanlder implementation to be used.
    • CookiePolicy.shouldAccept will be called by CookieManager.put to see whether or not one cookie should be accepted and put into cookie store. User can use any of three pre-defined CookiePolicy, namely ACCEPT_ALL, ACCEPT_NONE and ACCEPT_ORIGINAL_SERVER, or user can define his own CookiePolicy implementation and tell CookieManager to use it.
    • CookieStore is the place where any accepted HTTP cookie is stored in. If not specified when created, a CookieManager instance will use an internal in-memory implementation. Or user can implements one and tell CookieManager to use it.
    • Currently, only CookieStore.add(URI, HttpCookie) and CookieStore.get(URI) are used by CookieManager. Others are for completeness and might be needed by a more sophisticated CookieStore implementation, e.g. a NetscapeCookieStore.

    用户可以通过各种方式连接自己的HTTP cookie管理行为,例如

    • Use CookieHandler.setDefault to set a brand new CookieHandler implementation
    • Let CookieManager be the default CookieHandler implementation, but implement user's own CookieStore and CookiePolicy and tell default CookieManager to use them:
             // this should be done at the beginning of an HTTP session       CookieHandler.setDefault(new CookieManager(new MyCookieStore(), new MyCookiePolicy()));     
    • Let CookieManager be the default CookieHandler implementation, but use customized CookiePolicy:
             // this should be done at the beginning of an HTTP session       CookieHandler.setDefault(new CookieManager());       // this can be done at any point of an HTTP session       ((CookieManager)CookieHandler.getDefault()).setCookiePolicy(new MyCookiePolicy());     

    该实现符合RFC 2965 ,第3.3节。

    从以下版本开始:
    1.6
    另请参见:
    CookiePolicy
    • 构造方法详细信息

      • CookieManager

        public CookieManager()
        创建一个新的cookie管理器。

        此构造函数将使用默认cookie存储和接受策略创建新的cookie管理器。 效果与CookieManager(null, null)相同。

      • CookieManager

        public CookieManager​(CookieStore store,                     CookiePolicy cookiePolicy)
        使用指定的cookie存储和cookie策略创建新的cookie管理器。
        参数
        store - 由cookie管理员使用的CookieStore 如果是null ,cookie管理器将使用默认值,即内存中的CookieStore实现。
        cookiePolicy - cookie管理器用作策略回调的CookiePolicy实例。 如果使用null ,将使用ACCEPT_ORIGINAL_SERVER。
    • 方法详细信息

      • setCookiePolicy

        public void setCookiePolicy​(CookiePolicy cookiePolicy)
        设置此cookie管理器的cookie策略。

        默认情况下, CookieManager的实例将具有cookie策略ACCEPT_ORIGINAL_SERVER。 用户始终可以调用此方法来设置另一个cookie策略。

        参数
        cookiePolicy - cookie策略。 可以是null ,这对当前的cookie策略没有影响。
      • getCookieStore

        public CookieStore getCookieStore()
        检索当前cookie存储。
        结果
        cookie管理器当前使用的cookie存储。