Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 942
Pentium10 Avatar asked May 14 '10 12:05

Pentium10


People also ask

Can I delete cookie from another domain?

For security, you're not allowed to edit (or delete) a cookie on another site. Since there's no guarantee that you own both foo.domain.com and bar.domain.com , you won't be allowed to edit the cookies of foo.domain.com from bar.domain.com and vice versa.

How do I clear cookies on Webview Android?

Open your browser. Android browser: Go to Menu > More > Settings or Menu > Settings > Privacy & Security. Chrome: Go to Menu > Settings > Privacy. Android browser: Tap Clear cache, Clear history, and Clear all cookie data as appropriate.


1 Answers

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");
    }
}
like image 178
Tom Kincaid Avatar answered Oct 20 '22 23:10

Tom Kincaid