Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: BasicClientCookie/CookieStore adds quotes around cookie value

I'm using the following snippet of code to add a cookie to an http request in Android, sent using Android's DefaultHttpClient:

CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie2 cookie = new BasicClientCookie2("AUTH_TOKEN", "MY_TOKEN");
cookie.setVersion(1);
cookie.setDomain("my.domain.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);

context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

When I look at the received cookie value on the server, I get

Cookie: $Version=1; AUTH_TOKEN="MY_TOKEN"

but what I am expecting is (notice the missing quotes):

Cookie: $Version=1; AUTH_TOKEN=MY_TOKEN

Unfortunately, the extra quotes causes the server (which I don't manage) to choke, and completely ignore the cookie.

I tried using BasicClientCookie instead of BasicClientCookie2 with no luck. Is there a fix for this or am I missing something obvious?

like image 287
Jeshurun Avatar asked Jul 19 '26 16:07

Jeshurun


1 Answers

Apparently, calling cookie.setVersion(0); on the Cookie should fix this, as quotes are invalid in version 0 of the cookie spec according to this SO answer.

In my case, since this was the only cookie I had to send with the request, I simply added it as a header myself:

post.addHeader("Cookie", "AUTH_TOKEN=" + myAuthToken);

like image 113
Jeshurun Avatar answered Jul 22 '26 05:07

Jeshurun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!