views:

445

answers:

3

When using an HttpConnection in a BlackBerry app, you often get HTTP cookies in the response headers. Unfortunately there are no built-in APIs to assist with the parsing of the cookie headers.

Has anyone found a third-party library to assist with the parsing of the cookie header(s) into a more useful data object? Creating some custom code that just parses out the name and value of the cookie isn't too difficult, but I'd like to also consider other fields within the cookie such as the expiration and domain fields.

A: 

The cookies are just available in request and response headers. To add a cookie to a request, use URLConnection#addRequestProperty(), to get a cookie from a response (after actually sending the request!), use URLConnection#getHeaderField(). The header field syntax has to adhere the RFC 2965.

You can of course use a 3rd party API to abstract it all a bit more and make it all less low-level, a widely used one is Apache Commons HttpClient which provides under each the easy-to-use Cookie API. Handling cookies is described in this HttpClient guide. Some code example can be found here.

BalusC
Thanks but this is BlackBerry (J2ME) so HttpClient won't work here.
Marc Novakowski
Ah yes, then go ahead with parsing/creating it yourself conform the linkes RFC specs.
BalusC
A: 

Just to follow up on this topic - I just ended up writing my own code to do cookie parsing and persistence. I guess sometimes there just isn't an open-source library out there ready and waiting!

Marc Novakowski
A: 

Had you considered sharing your code? :) - I am in the process of writing exactly the same :/

Oz
Unfortunately since the code was developed for my employer, I can't release it publicly. If this changes, I'll be sure to update this entry with a link to the code.
Marc Novakowski