Android的CookieManager setCookie方法创建多个Cookie [英] Android CookieManager setCookie creates multiple cookies

查看:1805
本文介绍了Android的CookieManager setCookie方法创建多个Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我有一个web视图。它加载来自多个域的URL。我需要从特定域删除所有Cookie。我想保持饼干从其他域。但我需要从一个域删除所有cookie。我接受处理我的请求的所有其他解决方案。 (注意,域名使用HTTP和HTTPS)

In my android application I have a webview. It loads URLs from multiple domains. I need to delete all cookies from a specific domain. I want to keep cookies from other domains. But I need to delete all cookies from one domain. I'm open to all other solutions that handles my request. (note that domain uses both http and https)

但是,当我尝试使用CookieManager.setCookie,为该域的所有可用的cookie并没有被删除。多个cookie的键appeear当我试图写该密钥。

But when I try to use CookieManager.setCookie, all available cookies for that domain didn't deleted. Multiple cookie keys appeear when I try to write to that keys.

我在下面附上我的code。你可以在注释行的结果。在故事的结尾,我得到这个cookie。注意多个值:

I attach my code below. You can find results in comment lines. At the end of story I get this cookie. Note for multiple values:

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

一个分割cookie字符串来获得cookie的键

我的助手功能:

My helper function that splits cookie string to get cookie keys:

public static Vector<String> 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<String> allCookieField = new Vector<String>();
    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;
}

我得到present饼干:

I get present cookies:

// 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;

然后我打电话给取代旧的饼干。

Then I call to replace old cookies.

Vector<String> 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方法是这样的:


I also tried setCookie like this:

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

EDIT2:setCookie方法的签名是这样的:

setCookie's signature is like this:

 /**
 * 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字符串(用户ID = 12%,34内相同的密钥;记号= 12ased; remember_check = 0;用户ID = - 1;令牌= - 1'; remember_check =' -1;)将它们有不同的主机或路径

Although I get same keys inside cookie string ("userid=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';") will they have different host or path ?

推荐答案

我已经用的 CookieManager 在Android中。设置相同的cookie确实会添加为新的Cookie。

I've had a similar experience with the CookieManager in Android. Setting the same cookie will indeed add it as a new cookie.

请尝试实施该解决方案。它将使你能够刷新你想要的饼干删除,然后你就可以重新设置为你的愿望。

Please try to implement this solution. It will enable you to flush the cookies you want to remove and then you'll be able to set the again as you desire.

祝你好运!

这篇关于Android的CookieManager setCookie方法创建多个Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆