Most visited

Recently visited

Added in API level 9

CookieManager

public class CookieManager
extends CookieHandler

java.lang.Object
   ↳ java.net.CookieHandler
     ↳ java.net.CookieManager


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 NetscapeCookieSotre.

用户可以通过多种方式来连接他自己的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节。

也可以看看:

Summary

Public constructors

CookieManager()

创建一个新的cookie管理器。

CookieManager(CookieStore store, CookiePolicy cookiePolicy)

使用指定的Cookie存储和Cookie策略创建新的Cookie管理器。

Public methods

Map<StringList<String>> get(URI uri, Map<StringList<String>> requestHeaders)

从请求标头中指定的URI获取cookie缓存中的所有适用cookie。

CookieStore getCookieStore()

检索当前的Cookie存储。

void put(URI uri, Map<StringList<String>> responseHeaders)

设置所有适用的cookie,例如响应头中的响应头字段(名为Set-Cookie2),并显示在cookie缓存中。

void setCookiePolicy(CookiePolicy cookiePolicy)

设置此Cookie管理器的Cookie策略。

Inherited methods

From class java.net.CookieHandler
From class java.lang.Object

Public constructors

CookieManager

Added in API level 9
CookieManager ()

创建一个新的cookie管理器。

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

CookieManager

Added in API level 9
CookieManager (CookieStore store, 
                CookiePolicy cookiePolicy)

使用指定的Cookie存储和Cookie策略创建新的Cookie管理器。

Parameters
store CookieStore: a CookieStore to be used by cookie manager. if null, cookie manager will use a default one, which is an in-memory CookieStore implmentation.
cookiePolicy CookiePolicy: a CookiePolicy instance to be used by cookie manager as policy callback. if null, ACCEPT_ORIGINAL_SERVER will be used.

Public methods

get

Added in API level 9
Map<StringList<String>> get (URI uri, 
                Map<StringList<String>> requestHeaders)

从请求标头中指定的URI获取cookie缓存中的所有适用cookie。

作为参数传递的URI指定了Cookie的预期用途。 特别是该计划应反映cookie是通过http,https发送还是用于其他背景(如javascript)。 主机部分应该反映cookie的目的地或者javascript的来源。

考虑到 URI以及cookie属性和安全设置以确定哪些应该返回,这取决于实施。

HTTP协议实现者应确保在添加了与选择cookie相关的所有请求标头之后并在请求发送之前调用此方法。

Parameters
uri URI: a URI representing the intended use for the cookies
requestHeaders Map: - a Map from request header field names to lists of field values representing the current request headers
Returns
Map<StringList<String>> an immutable map from state management headers, with field names "Cookie" or "Cookie2" to a list of cookies containing state information
Throws
IOException

getCookieStore

Added in API level 9
CookieStore getCookieStore ()

检索当前的Cookie存储。

Returns
CookieStore the cookie store currently used by cookie manager.

put

Added in API level 9
void put (URI uri, 
                Map<StringList<String>> responseHeaders)

设置所有适用的cookie,例如响应头中的响应头字段(名为Set-Cookie2),并显示在cookie缓存中。

Parameters
uri URI: a URI where the cookies come from
responseHeaders Map: an immutable map from field names to lists of field values representing the response header fields returned
Throws
IOException

setCookiePolicy

Added in API level 9
void setCookiePolicy (CookiePolicy cookiePolicy)

设置此Cookie管理器的Cookie策略。

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

Parameters
cookiePolicy CookiePolicy: the cookie policy. Can be null, which has no effects on current cookie policy.

Hooray!