16

in my application, I'm getting two cookies from an HttpGet request and store them in the CookieManager like this:

//Clear old cookies
CookieManager.getInstance().removeAllCookie();
CookieSyncManager.getInstance().sync();

//Save the two cookies: auth token and session info
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies != null) {
    for (Cookie cookie : cookies) {
        String cookieString = cookie.getName() + "=" + cookie.getValue() + "; Domain=" + cookie.getDomain();
        CookieManager.getInstance().setCookie("http://alpha.mydomainname.com", cookieString);
    }

    System.out.println(CookieManager.getInstance().hasCookies()); //Prints false in 2.3,  true in 4.0.3

    CookieSyncManager.getInstance().sync();

    System.out.println(CookieManager.getInstance().hasCookies()); //Also prints false in 2.3 and true in 4.0.3
}

I'm testing the same code in two different devices and the funny thing is, the cookies are set (and also transferred between launches of the application) correctly in 4.0.3 but not in 2.3.3. When I say they are not set, I mean that hasCookies() returns false and also getCookie() returns null when I provide the URL.

I've tried every possible combination for the Cookie URL when calling setCookie: "http://alpha.mydomainname.com", "http://www.mydomainname.com", "http://mydomainname.com", "mydomainname.com", "alpha.mydomainname.com", ".mydomainname.com", "www.mydomainname.com", none of them works. Please help.

Ayberk Özgür
  • 4,986
  • 4
  • 38
  • 58
  • 1
    did you ever get this working? I"m running into the same issue now... – Christopher Johnson Dec 18 '12 at 23:22
  • 1
    I need a few more tries to be sure, but I'm finding something like the following: if the Expires header is set, the cookie saves on 2.3.3 but not 4.0.4, whereas if Expires is missing the cookie saves on 4.0.4 but not 2.3.3. – hly Jan 22 '13 at 14:48
  • Have you solved your problem? I'm facing the same here.... – Pedro Teran Oct 08 '13 at 15:12
  • See also https://stackoverflow.com/questions/1652850/android-webview-cookie-problem. In my case I wrote usual URL (`https://example.com/`) as a domain. In some cases it would be better set `.example.com`. – CoolMind Jun 18 '20 at 09:32

4 Answers4

6

I recently had a similar problem, and the following solution worked for me. I create/get instances of CookieSyncManager and CookieManager at the beginning, and use them throughout the code, instead of creating new instances again. I also had to experiment with setting the cookie on the correct domain - I had to set it to the domain that appears in one of the redirects.

final CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
final CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();

//Save the two cookies: auth token and session info
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies != null) {
    for (Cookie cookie : cookies) {
        String cookieString = cookie.getName() + "=" + cookie.getValue() + "; Domain=" + cookie.getDomain();
        cookieManager.setCookie("http://mydomainname.com", cookieString);
    }
    cookieSyncManager.sync();
}
synapseError
  • 126
  • 1
  • 8
5

I've faced the same problem. There is not so clear how to use the setCookie method. You should use it with some loop if you have a few items with cookies (like in my case):

val cookies = "key1=someValue1;key2=someValue2;key3=someValue3"
val cookiesList = cookies.split(";")
cookiesList.forEach { item ->
 CookieManager.getInstance().setCookie("http://someHost.com", item)
}

So you can't use it like:

CookieManager.getInstance().setCookie("http://someHost.com", "key1=someValue1;key2=someValue2;key3=someValue3")
Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Djek-Grif
  • 1,391
  • 18
  • 18
0

I also ran into some strange behaviours with CookieManager, eventually I ended up with a solution that is a workaround - but it works.
Instead of using CookieManager I just used the http cookie headers, so for example using HttpUrlConnection it can look like that:

//Save the two cookies: auth token and session info
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
    String cookieString = cookie.getName() + "=" + cookie.getValue();
    myHttpURLConnection.setRequestProperty("Cookie", cookieString);
}

Of course, if you need to persist these cookies you will have to do it wourself and save their value somewhere for later use...

Inon Stelman
  • 955
  • 7
  • 12
0

The cookie can't include the semicolon, because the semicolon means separator in cookies.

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
AnswerZhao
  • 366
  • 3
  • 13