Search code examples
androidcookieswebkit

How to remove cookies using CookieManager for a specific domain?


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?


Solution

  • 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.