在我的 Android 应用程序中,我有一个网络视图。它从多个域加载 URL。我需要删除特定域中的所有 cookie。我想保留来自其他域的 cookie。但我需要删除一个域中的所有 cookie。我对处理我的请求的所有其他解决方案持开放态度。 (请注意,域同时使用 http 和 https)
但是当我尝试使用 CookieManager.setCookie 时,该域的所有可用 cookie 都没有删除。当我尝试写入这些键时,会出现多个 cookie 键。
我在下面附上我的代码。您可以在注释行中找到结果。在故事的最后我得到了这个饼干。多个值的注意事项:
"userid=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';"
我的辅助函数分割 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;
}
我收到礼物饼干:
// 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<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';"
}
Edit:
我也尝试过像这样设置Cookie:
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=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';"
)他们会有不同的主机或路径吗?