I want to logout my twitter account by deleting the cookies created by it. I am able to retrive the cookies created by twitter using code:
String twit_cookie = getCookie ("http://www.twitter.com");
But how can i delete only cookies created by twitter because removeAllCookie() deletes all the cookies created by browser. How can i delete the specific cookie by URL or by name???
Please help...
CookieManager class has a method setCookie. Have you tried it like:
setCookie("http://www.twitter.com", null);
Or perhaps
setCookie("http://www.twitter.com", "auth_token=''");
You can use the method CookieManager#setCookie(String url, String value). As stated in the docs:
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 "clearest" way is to set all cookies created by twitter to expired (a time in the past). The code from this answer is almost right, except the date is in the future.
Modified code:
final String domain = "http://www.twitter.com";
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
String cookiestring = cookieManager.getCookie(domain); //get all cookies
String[] cookies = cookiestring.split(";");
for (int i=0; i<cookies.length; i++) {
String[] cookieparts = cookies[i].split("="); //split cookie into name and value etc.
// set cookie to an expired date
cookieManager.setCookie(domain, cookieparts[0].trim()+"=; Expires=Wed, 31 Dec 2000 23:59:59 GMT");
}
CookieSyncManager.getInstance().sync(); //sync the new cookies just to be sure
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With