Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all cookies from specific domain in web view

I am trying to remove all the cookies from some domain, is there a way to do this?

The only method i saw is removeAllCookies.

Thank you.

like image 653
nahum Avatar asked Feb 14 '16 10:02

nahum


People also ask

Can you clear Cookies for one site?

Select the lock icon next to a website name in the Address bar, then select Cookies. In the Cookies in use dialog box, expand a site name, choose a cookie, then select Remove.


3 Answers

As per documentation, we don't have a method for removing individual cookies. But we could use an interesting work around with setCookie() to clear a site's cookies. as,

public abstract void setCookie (String url, String value)

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 cookie being set will be ignored if it is expired.

So, the solution is, As mentioned in this answer, we need to manually clean up each cookie for each host key. eg:- facebook

android.webkit.CookieManager.getInstance().setCookie(".facebook.com", "locale=");
android.webkit.CookieManager.getInstance().setCookie(".facebook.com", "datr=");
android.webkit.CookieManager.getInstance().setCookie(".facebook.com", "s=");
android.webkit.CookieManager.getInstance().setCookie(".facebook.com", "csm=");
android.webkit.CookieManager.getInstance().setCookie(".facebook.com", "fr=");
android.webkit.CookieManager.getInstance().setCookie(".facebook.com", "lu=");
android.webkit.CookieManager.getInstance().setCookie(".facebook.com", "c_user=");
android.webkit.CookieManager.getInstance().setCookie(".facebook.com", "xs=");
like image 143
Let'sRefactor Avatar answered Oct 16 '22 10:10

Let'sRefactor


First of all I can suggest you to have a look at CookieManager class which manages the cookies used by WebView. There was a method removeAllCookie() for clearing the cookies but It was deprecated in API level 21, So instead of this you can use removeAllCookeis(callback) method. You can pass null as the callback if you don't need to know when the operation completes or whether any cookie were removed, and in this case it is safe to call the method from a thread without a Looper.

To clear cookies in lollipop or more you can use

CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();

and in rest

CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context);
cookieSyncMngr.startSync();
CookieManager cookieManager=CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncMngr.stopSync();
cookieSyncMngr.sync();
like image 5
Shree Krishna Avatar answered Oct 16 '22 11:10

Shree Krishna


Let'sRefactor and AnT answers were helpful to find final solution for me. So basically we need to clean every cookie, one by one. But for me just setting "value=" or "value=; Domain=my.domain.com" was not working. I guess it is a problem of the page I was loading, on which I had no control (it was printing crash stack trace on reading the cookies). So for me final solution was to remove cookies, not empty them. Of course we don't have any API for deleting particular cookie, but we can use parameter of 'Set-Cookie' HTTP response header format for Max-Age. My final code looks like this (Kotlin, in production code I have strings in constants of course ;) ):

val domain = myPageUri.host
CookieManager.getInstance().apply {
   getCookie(domain)
      .split("; ")
      .filter { it.isNotNullOrBlank() }
      .forEach {
         val newCookieValue = it.substringBefore("=") + "=; Max-Age=-1"
         setCookie(domain, newCookieValue)
      }
}

substringBefore(character: String) is just our extension function on String to return substring from start to first occurrence of character.

like image 4
Dariusz Wiechecki Avatar answered Oct 16 '22 09:10

Dariusz Wiechecki