Android中的CookieManager

android.webkit
类 CookieManager

java.lang.Object
  继承者 android.webkit.CookieManager

public final class CookieManagerextends Object

CookieManager manages cookies according to RFC2109 spec.


方法摘要
 booleanacceptCookie() 
          Return whether cookie is enabled
 StringgetCookie(String url) 
          Get cookie(s) for a given url so that it can be set to "cookie:" in http request header.
 StringgetCookie(WebAddress uri) 
          Get cookie(s) for a given uri so that it can be set to "cookie:" in http request header.
static CookieManagergetInstance() 
          Get a singleton CookieManager.
 booleanhasCookies() 
          Return true if there are stored cookies.
 voidremoveAllCookie() 
          Remove all cookies
 voidremoveExpiredCookie() 
          Remove all expired cookies
 voidremoveSessionCookie() 
          Remove all session cookies, which are cookies without expiration date
 voidsetAcceptCookie(boolean accept) 
          Control whether cookie is enabled or disabled
 voidsetCookie(String url, String value) 
          Set cookie for a given url.
 voidsetCookie(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 for
value - 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 for
value - 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();  
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值