在我的android應用程序中,我有一個webview。它從多個域加載URL。我需要刪除特定域中的所有Cookie。我想保留來自其他域的Cookie。但我需要從一個域中刪除所有Cookie。我願意接受處理我請求的所有其他解決方案。 (注意該域使用http和https)Android CookieManager setCookie創建多個Cookie
但是,當我嘗試使用CookieManager.setCookie時,該域的所有可用的cookie都沒有被刪除。當我嘗試寫入該密鑰時,會出現多個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;
}
我得到禮物餅乾:
"userid=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';"
其將cookie字符串得到的cookie鑰匙我的助手功能
// 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';"
}
編輯:
我也試過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=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';"
)他們會有不同的主機或路徑?
我試過了過期的方法,仍然沒有去。之後我會調用removeExpiredCookie,並且Cookie仍然保持不變。另外,當您將過期時間添加到cookie值時,我認爲setCookie會忽略它。 – darewreck