5

Lets prepare for cookie storing:

CookieSyncManager.createInstance(getApplicationContext());
CookieSyncManager.getInstance().startSync();
CookieManager.getInstance().setAcceptCookie(true);

Then I'm putting manually some cookies, lets say PHPSESSID and RANDOM

CookieManager.getInstance().setCookie("domain.com", "PHPSESSID="+phpSession);
CookieManager.getInstance().setCookie("domain.com", "RANDOM="+random);

lets check is it working using:

CookieManager.getInstance().getCookie("domain.com");

and got

PHPSESSID=dba4ff392agd39b5951d10a91a0a7b56; RANDOM=266284790:1466147978:c91d0896bac59e0b

Everything looks good, but when I navigate in may app to one of WebView Activities, which are opening same domain website also setting cookies, then when I print cookie like above it looks like this:

PHPSESSID=dba4ff392agd39b5951d10a91a0a7b56;
RANDOM=266284790:1466147978:c91d0896bac59e0b;
PHPSESSID=9ecb5156cf8fc3190fbc69fd13393243;
RANDOM=265078219%3A1463147975%3Ad0448d163e9b2123

duplicated entries... when after that I manually set again e.g. RANDOM with setCookie:

PHPSESSID=dba4ff392agd39b5951d10a91a0a7b56; 
RANDOM=111111111:2222222222:33333336bac59e0b;
PHPSESSID=9ecb5156cf8fc3190fbc69fd13393243; 
RANDOM=265078219%3A1463147975%3Ad0448d163e9b2123

values set by WebView are not overwritten, only my "manually" entered... how to force WebView to use my earlier set cookie OR overwrite already set?

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Have you found the reason of this behaviour? – Iwo Banas Mar 17 '17 at 14:43
  • I've ended up with checking on (first) start that cookie exists, if not then I'm opening `WebView` (created "dynamicly" in `Activity` by `new WebView(this)`, size 1x1) with url, which is intended only to set cookie. after that inside app I'm using this cookie for webapi requests. not so well fixed IMHO, but I didn't found better solution :/ – snachmsm Mar 21 '17 at 10:37

2 Answers2

3

As in the MDN docs about Set-Cookie you can see many different kinds of cookie values, a cookie can be set to particular Path

cookie-name=cookie-value; Path=path-value

And in CookieManager.setCookie void setCookie (String url, String value), the android reference says:

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.

In my opinion, the reason that you have duplicate entries is because the cookie values were in different Path. So if you want to overwrite you should make sure host path name are the same.

ryanhex53
  • 577
  • 4
  • 15
0
  private void setCookieManager(String auth_token) {
    CookieSyncManager manager = CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.removeSessionCookie();
    cookieManager.setCookie(auth_token);

    manager.sync();
}

I think you just didn't setup your cookie manager properly. Give this a try

paul_hundal
  • 371
  • 2
  • 9
  • I'm currently giving this a try like above... I don't want to `removeSessionCookie();`, it should be kept, and I'm syncing (`manager.sync();`) after every manual insertion and in `onPause` in activites with `WebView` – snachmsm May 16 '16 at 07:32