I know about the existince of CookieManager, but how do I remove cookies of a domain only?
Can someone help me with some code fragment?
Call android.webkit.CookieManager
's getCookie
method to generate a RFC 2109 Cookie
header for the URL or domain you are interested. Parse the cookie header to get a list of cookie names. For each cookie name, generate a RFC 2109 Set-Cookie
header for that name that has an expiry date in the past and pass it into CookieManager
's setCookie
method. Although the API docs specify that setCookie
ignores values that have expired, Android's current implementation actually flushes the cookie in this case. To guard against future implementations that do ignore expired values as specified in the documentation, check that the cookies were actually removed and perform some fallback behaviour if they haven't—CookieManager
's removeAllCookie
method may be useful for this fallback.
I don't see anyway to do this in the API, but you can always dig into the real source code (open source is nice)... for example, I found this deleteCookies
method in this class: WebViewDatabase which is part of the core of Android.
As you can see there... cookies are just rows into a SQLite Database... so if you can make this class work, at least you know how to do it by your self.
Here is a code sample from an open source project. Maybe could help someone.
https://github.com/janrain/engage.android/blob/96f21b45738ef82a911e27d8a707aff3a1024d36/Jump/src/com/janrain/android/utils/WebViewUtils.java
private static void deleteWebViewCookiesForDomain(Context context, String domain, boolean secure) {
CookieSyncManager csm = CookieSyncManager.createInstance(context);
CookieManager cm = CookieManager.getInstance();
/* http://code.google.com/p/android/issues/detail?id=19294 */
if (AndroidUtils.SDK_INT >= 11) {
// don't trim leading '.'s
} else {
/* Trim leading '.'s */
if (domain.startsWith(".")) domain = domain.substring(1);
}
/* Cookies are stored by domain, and are not different for different schemes (i.e. http vs
* https) (although they do have an optional 'secure' flag.) */
domain = "http" + (secure ? "s" : "") + "://" + domain;
String cookieGlob = cm.getCookie(domain);
if (cookieGlob != null) {
String[] cookies = cookieGlob.split(";");
for (String cookieTuple : cookies) {
String[] cookieParts = cookieTuple.split("=");
/* setCookie has changed a lot between different versions of Android with respect to
* how it handles cookies like these, which are set in order to clear an existing
* cookie. This way of invoking it seems to work on all versions. */
cm.setCookie(domain, cookieParts[0] + "=;");
/* These calls have worked for some subset of the the set of all versions of
* Android:
* cm.setCookie(domain, cookieParts[0] + "=");
* cm.setCookie(domain, cookieParts[0]); */
}
csm.sync();
}
}
public void clearCookies(String domain) {
CookieManager cookieManager = CookieManager.getInstance();
String cookiestring = cookieManager.getCookie(domain);
String[] cookies = cookiestring.split(";");
for (int i=0; i<cookies.length; i++) {
String[] cookieparts = cookies[i].split("=");
cookieManager.setCookie(domain, cookieparts[0].trim()+"=; Expires=Wed, 31 Dec 2025 23:59:59 GMT");
}
}