Reputation: 5300
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.
Upvotes: 17
Views: 28967
Reputation: 105
I found this post when I couldn't get setCookie to update existing cookies (it would instead create a duplicate cookie of the same name), and that was because those cookies were using CHIPS with the Partitioned; value. Removing Partitioned from the Set-Cookie http response header made those cookies editable again.
Upvotes: 0
Reputation: 376
The cookie can't include the semicolon, because the semicolon means separator in cookies.
Upvotes: 0
Reputation: 1546
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")
Upvotes: 10
Reputation: 126
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();
}
Upvotes: 6
Reputation: 965
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...
Upvotes: 0