android.webkit
类 CookieManager
java.lang.Objectandroid.webkit.CookieManager
public final class CookieManagerextends Object
CookieManager manages cookies according to RFC2109 spec.
方法摘要 | |
---|---|
boolean | acceptCookie() Return whether cookie is enabled |
String | getCookie(String url) Get cookie(s) for a given url so that it can be set to "cookie:" in http request header. |
String | getCookie(WebAddress uri) Get cookie(s) for a given uri so that it can be set to "cookie:" in http request header. |
static CookieManager | getInstance() Get a singleton CookieManager. |
boolean | hasCookies() Return true if there are stored cookies. |
void | removeAllCookie() Remove all cookies |
void | removeExpiredCookie() Remove all expired cookies |
void | removeSessionCookie() Remove all session cookies, which are cookies without expiration date |
void | setAcceptCookie(boolean accept) Control whether cookie is enabled or disabled |
void | setCookie(String url, String value) Set cookie for a given url. |
void | setCookie(WebAddress uri, String value) Set cookie for a given uri. |
从类 java.lang.Object 继承的方法 |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
方法详细信息 |
---|
getInstance
public static CookieManager getInstance()
-
Get a singleton CookieManager. If this is called before any WebView is created or outside of WebView context, the caller needs to call CookieSyncManager.createInstance(Context) first.
-
-
返回:
- CookieManager =
setAcceptCookie
public void setAcceptCookie(boolean accept)
-
Control whether cookie is enabled or disabled
-
-
参数:
accept
- TRUE if accept cookie
acceptCookie
public boolean acceptCookie()
-
Return whether cookie is enabled
-
-
返回:
- TRUE if accept cookie
setCookie
public void setCookie(String url, String value)
-
Set cookie for a given url. The old cookie with same host/path/name will be removed. The new cookie will be added if it is not expired or it does not have expiration which implies it is session cookie.
-
-
参数:
url
- The url which cookie is set forvalue
- The value for set-cookie: in http response header
setCookie
public void setCookie(WebAddress uri, String value)
-
Set cookie for a given uri. The old cookie with same host/path/name will be removed. The new cookie will be added if it is not expired or it does not have expiration which implies it is session cookie.
-
-
参数:
uri
- The uri which cookie is set forvalue
- The value for set-cookie: in http response header
getCookie
public String getCookie(String url)
-
Get cookie(s) for a given url so that it can be set to "cookie:" in http request header.
-
-
参数:
url
- The url needs cookie 返回:- The cookies in the format of NAME=VALUE [; NAME=VALUE]
getCookie
public String getCookie(WebAddress uri)
-
Get cookie(s) for a given uri so that it can be set to "cookie:" in http request header.
-
-
参数:
uri
- The uri needs cookie 返回:- The cookies in the format of NAME=VALUE [; NAME=VALUE]
removeSessionCookie
public void removeSessionCookie()
-
Remove all session cookies, which are cookies without expiration date
-
removeAllCookie
public void removeAllCookie()
-
Remove all cookies
-
hasCookies
public boolean hasCookies()
-
Return true if there are stored cookies.
-
removeExpiredCookie
public void removeExpiredCookie()
- Remove all expired cookies
在Android应用程序中经常会加载一个WebView页,如果需要客户端向WebView传递信息,比如Cookie,也是可以的。
需要应用程序先将Cookie注入进去,打开该网页时,WebView会将加载的url通过http请求传输到服务器。同时,在这次请求中,会将Cookie信息通过http header传递过去。
流程如下:
1、客户端通过以下代码设置cookie
public static void synCookies(Context context, String url) { CookieSyncManager.createInstance(context); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setCookie(url, "uid=1243432"); CookieSyncManager.getInstance().sync(); }
2、CookieManager会将这个Cookie存入该应用程序/data/data/databases/目录下的webviewCookiesChromium.db数据库的cookies表中
3、打开网页,WebView从数据库中读取该cookie值,放到http请求的头部,传递到服务器
4、客户端可以在注销登录时清除该应用程序用到的所有cookies
private void removeCookie(Context context) { CookieSyncManager.createInstance(context); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.removeAllCookie(); CookieSyncManager.getInstance().sync(); }