如何使用CookieManager为特定域删除Cookie? [英] How to remove cookies using CookieManager for a specific domain?
问题描述
我知道 CookieManager 的存在,但是如何仅删除域的Cookie?
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?
推荐答案
调用 android.webkit.CookieManager
的 getCookie
方法生成RFC 2109 Cookie
为您感兴趣的网址或域的标头。解析Cookie头以获取Cookie名称列表。对于每个cookie名称,为过去有过期日期的那个名称生成一个RFC 2109 Set-Cookie
头,并将其传递给 CookieManager
'
setCookie
方法。虽然API文档指定 setCookie
忽略已过期的值,但Android的当前实现实际上会刷新此情况下的Cookie。为了防止未来实现忽略文档中指定的过期值,请检查Cookie是否实际删除,如果没有 - CookieManager
',则执行一些回退行为 removeAllCookie
方法可能对此回退有用。
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.
这篇关于如何使用CookieManager为特定域删除Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!