Manage Cookies with BlackBerry Java

When interacting with [generally and widely interpreted] web services, you may be required to communicate state (usually a login authentication token). This [usually] takes the form of a cookie and in our interconnected wide world of interwebs, browsers handle cookies just fine; custom-built clients not always. On the BlackBerry, if you’re putting together a Java app, and using the HttpConnection class to communicate with said server, you need to manage that cookie (and hence state) by yourself. Fortunately, it’s not rocket science.

The basic workflow is: you connect to the endpoint and POST some data. On the response, you need to read the headers and look for the “Set-Cookie” field. From that value, you can extra the data you need. On all subsequent requests, you set the “Cookie” field on the header. Simple. Time for some code.

NOTE: Exception handling omitted for brevity and readability.

The first request is just a regular POST in this instance. The buffer variable is a byte[].

HttpConnectionFactory factory = new HttpConnectionFactory(endpoint);
HttpConnection connection = factory.getNextConnection();
connection.setRequestProperty("User-Agent", "BlackBerry Client");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream output = connection.openOutputStream();

From that request, we wait for the response and read that. In this example, I am looking for the PHPSESSID field that was set as part of the response in the header.

String cookie = connection.getHeaderField("Set-Cookie");
if(null != cookie) {
  int php = cookie.indexOf("PHPSESSID=");
    if(0 <= php) {
      php += "PHPSESSID=".length();
      int term = cookie.indexOf(";", php);
      if(0 <= term) {
        _session = cookie.substring(php, term);

Now I have the token required (cookie) that I can now send on every subsequent request. The code for that is for all intensive purpose, the same as the first code snippet with one additional line:

connection.setRequestProperty("Cookie", "PHPSESSID=" + _session + ";");

The order of the setting of the request properties won’t make a difference, so just use that anywhere before you open the output stream for writing.



Started working with AppEngine a little bit recently and for the most part, I’m impressed by the ability to just get on with it. It resonated with some of Rob’s post and his idea of “stop thinking, just build” (with the ‘stop thinking’ part given a reasonable and relaxed interpretation on my part). And even jumping into Python again was fairly easy (read ‘nice’). Admittedly, my journey with Ruby has helped big time in overcoming the “who’s-moved-my-cheese” factor when it comes to static typing, intellisense, IDE… well, all the “trappings” if you like 🙂 So, back to AppEngine.

It’s fast, simple and uncomplicated. Some straightforward abstractions and useful mocks (like the user login for example) and even without having to host an app on Google, using the tool chain locally is superb for tracer bullets- even quicker than, dare i say, Rails? 🙂

Once, that is, you get over with the conventions of the platform and language (which is not huge by any stretch of the imagination). And on that note, i just want to state categorically that conventions are great. Not the kind where you drink good coffee and chat much g33k. The kind where you make a whole bunch of assumptions up front and then code everything according to those assumptions (and a further step of faith: you wire those assumptions into the framework). Yes, i know what they say about assumptions making something out of U and ME. A quirky expression- but is that really an absolute truth? And I’ve noted a different kind of assumptions before.

I feel this is where some of the mainstream programming gets lost from time to time (and with good reason mind you). Anything can happen, everything goes- no real conventions: you get to code *everything*. Joy. (All) you have are classes which you can abuse to perform functions and in between you get to do a lot of heavy lifting. Then again, what’s heavy is relative.

And in the web application world, AppEngine does a lot of the heavy. Which is great for just getting on with it. Try it. Go on… 60 minutes is all it’ll take.

Rest [as defined by a techie]

so life in the land of .net has been a little quiet. there’s been a gap in our project which i have been making the most of to get my head around some *other* things 😀
so, the backend and data coding has been burning slowly, but what has been happening has been interesting.. adventures in the land of PHP, JSON and Ajax…
luckily, i had a project to help keep things *focused* [ my beautiful ] 😀
the most facinating piece of software to emerge from all this was: Timeline. Wow.
I’ve used it here:
Pregnancy Due Dates and it really is an awesome implementation….
right! back to learning 🙂

Google vs Microsoft

A bit iffy really that two huge companies war with each other in the way they do… {sigh}
But then again, on the other side of the {sigh} is a…
cool. it’s what makes companies healthy, string.. heck.. everything gets stronger with a little bit of sparring.

But the “war” reached my doorstep with Google’s recent toolbar update. I clicked the OK informational informing it was updated.. and then…

Ad-Watch recorded +4300 events within seconds! Dang! I thought i was under attack until i read the notification [they were all the same]:

Registry modification detected
Key: Software\Microsoft\Internet Explorer\Main
Value: Search Page
New Data:

IE and Google battling it out to be the default search on my lone little machine.. :D… i had to engage in fairly lo-level advanced user diagnostics to stop my registry from being assualted and my machine slowing down- between IE, Google Toolbar and Ad-Watch, the discussions were getting pretty heavy.

Hey, Google.. How about letting the user decide who wins instead of trying to force your way in? It’s like you think you own my PC just because i chose to use your software? {gggrrrr}

Irritated is all.