Android多个cookie,Android CookieManager setCookie创建多个cookie

在我的

Android应用程序中,我有一个webview.它从多个域加载URL.我需要删除特定域中的所有cookie.我想保留其他域名的cookie.但我需要删除一个域中的所有cookie.我对处理我的请求的所有其他解决方案持开放态度. (请注意,域使用http和https)

但是当我尝试使用CookieManager.setCookie时,该域的所有可用cookie都没有删除.当我尝试写入该键时,多个cookie键会出现.

我在下面附上我的代码.您可以在评论行中找到结果.在故事的结尾我得到这个cookie.注意多个值:

"userid=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';"

我的帮助函数,它分割cookie字符串以获取cookie密钥:

public static Vector getCookieAllKeysByCookieString(String pCookies) {

if (TextUtils.isEmpty(pCookies)) {

return null;

}

String[] cookieField = pCookies.split(";");

int len = cookieField.length;

for (int i = 0; i < len; i++) {

cookieField[i] = cookieField[i].trim();

}

Vector allCookieField = new Vector();

for (int i = 0; i < len; i++) {

if (TextUtils.isEmpty(cookieField[i])) {

continue;

}

if (!cookieField[i].contains("=")) {

continue;

}

String[] singleCookieField = cookieField[i].split("=");

allCookieField.add(singleCookieField[0]);

}

if (allCookieField.isEmpty()) {

return null;

}

return allCookieField;

}

我得到礼物饼干:

// I take cookie string for specific URL

mCookieManager = CookieManager.getInstance();

String url2="https://mysite.com";

String cookieString = mCookieManager.getCookie(url2);

Toast.makeText(mContext, "cookie string:\n"+cookieString, Toast.LENGTH_SHORT).show();

// result is: userid=12%34; token=12ased; remember_check=0;

然后我打电话来换掉旧的饼干.

Vector cookie = CookieUtil.getCookieAllKeysByCookieString(cookieString);

if (cookie == null || cookie.isEmpty()) {

Toast.makeText(mContext, "cookie null", Toast.LENGTH_SHORT).show();

}

if (cookie != null) {

int len = cookie.size();

Toast.makeText(mContext, "cookie number: "+len, Toast.LENGTH_SHORT).show();

// result is, cookie number: 3

String cookieNames="";

for (int i = 0; i < len; i++) {

cookieNames += "\n"+cookie.get(i) ;

mCookieManager.setCookie(url2, cookie.get(i) + "='-1';");

}

Toast.makeText(mContext, "cookieNames:\n"+cookieNames, Toast.LENGTH_SHORT).show();

// result is: "cookienames: userid token remember_check"

mCookieSyncManager.sync();

cookieString = mCookieManager.getCookie(url2);

Toast.makeText(mContext, "cookie string:\n"+cookieString, Toast.LENGTH_SHORT).show();

mCookieSyncManager.sync();

// result is: "userid=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';"

}

编辑:

我也试过像这样的setCookie:

mCookieManager.setCookie(url2, cookie.get(i) + "=-1;");

mCookieManager.setCookie(url2, cookie.get(i) + "=-1");

Edit2:setCookie的签名是这样的:

/**

* Sets a cookie for the given URL. Any existing cookie with the same host,

* path and name will be replaced with the new cookie. The cookie being set

* must not have expired and must not be a session cookie, otherwise it

* will be ignored.

*

* @param url the URL for which the cookie is set

* @param value the cookie as a string, using the format of the 'Set-Cookie'

* HTTP response header

*/

public void setCookie(String url, String value) {

throw new MustOverrideException();

}

虽然我在cookie字符串中获得了相同的键(“userid = 124; token = 12ased; remember_check = 0; userid =’ – 1′; token =’ – 1′; remember_check =’ – 1′;”)但是它们会有不同的主机还是路径?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值