public class CookieManager extends CookieHandler
CookieHandler
cookiemanager提供具体的实施,使饼干存储从周围的接受和拒绝cookies政策。一个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 ownCookieStore
andCookiePolicy
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 customizedCookiePolicy
:// 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节。
CookiePolicy
Constructor and Description |
---|
CookieManager()
创建一个新的饼干管理器。
|
CookieManager(CookieStore store, CookiePolicy cookiePolicy)
创建一个新的饼干管理器与指定的饼干店和饼干政策。
|
Modifier and Type | Method and Description |
---|---|
Map<String,List<String>> |
get(URI uri, Map<String,List<String>> requestHeaders)
从一个请求标头中指定的URI的cookie缓存中获取所有适用的饼干。
|
CookieStore |
getCookieStore()
要检索当前的饼干店。
|
void |
put(URI uri, Map<String,List<String>> responseHeaders)
集所有适用的饼干,例如响应头域,被命名为set-cookie2,目前在响应头到一个cookie缓存。
|
void |
setCookiePolicy(CookiePolicy cookiePolicy)
要设置此饼饼管理器的“饼干”策略。
|
getDefault, setDefault
public CookieManager()
此构造函数将创建新的具有默认的饼干存储和接受策略的饼干管理器。效果是一样的CookieManager(null, null)
。
public CookieManager(CookieStore store, CookiePolicy cookiePolicy)
store
-
CookieStore
被Cookie管理器使用。如果
null
,cookie管理器将使用默认的,这是一个内存存储机制的实现。
cookiePolicy
-
CookiePolicy
实例可以通过Cookie管理政策回调。如果
null
,accept_original_server将使用。
public void setCookiePolicy(CookiePolicy cookiePolicy)
一审CookieManager
将默认cookie政策accept_original_server。用户总是可以调用此方法来设置另一个“饼干”策略。
cookiePolicy
的cookie政策。可以
null
,这对当前的cookie政策没有影响。
public CookieStore getCookieStore()
public Map<String,List<String>> get(URI uri, Map<String,List<String>> requestHeaders) throws IOException
CookieHandler
作为一个参数传递的URI
指定用途的饼干。具体的方案应该反映是否饼干将发送HTTP、HTTPS或用在另一种情况下,如JavaScript。主机部分应反映目标的饼干或者在JavaScript的案件来源。
要实现考虑到URI
和饼干的属性和安全设置,以确定哪些应该归还。
HTTP协议的实施者应确保该方法添加后都选择饼干相关请求报头,并发送请求之前。
get
方法重写,继承类
CookieHandler
uri
-
URI
代表使用的饼干
requestHeaders
-Map从请求标头字段名称列表的字段的值表示当前请求报头
IOException
如果I/O错误发生
CookieHandler.put(URI, Map)
public void put(URI uri, Map<String,List<String>> responseHeaders) throws IOException
CookieHandler
put
方法重写,继承类
CookieHandler
uri
-
URI
饼干是从哪里来的
responseHeaders
从字段名称列表字段值代表响应头域不变的Map返回
IOException
如果I/O错误发生
CookieHandler.get(URI, Map)
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.