3

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?

2 Answers 2

4

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.