Android CookieManager setCookie创建多个cookie

tra*_*nte 6 cookies mobile android

在我的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';"
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

我得到礼物饼干:

// 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;
Run Code Online (Sandbox Code Playgroud)

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

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';"
}
Run Code Online (Sandbox Code Playgroud)

编辑:
我也尝试过setCookie:

mCookieManager.setCookie(url2, cookie.get(i) + "=-1;");
mCookieManager.setCookie(url2, cookie.get(i) + "=-1");
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

虽然我在cookie string("userid=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';")中获得相同的键,但它们会有不同的主机或路径吗?

Ass*_*iel 3

我对CookieManager有过类似的经历。设置相同的 cookie 确实会将其添加为新的 cookie。

请尝试实施此解决方案。它将使您能够获得flush您想要的 cookieremove,然后您就可以根据需要重新设置。

祝你好运!