Question

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?

Was it helpful?

Solution

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);

OTHER TIPS

RFC 6265:

cookie-header = "Cookie:" OWS cookie-string OWS
cookie-string = cookie-pair *( ";" SP cookie-pair )
cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )

So user-agent MAY place quotes around value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top