Java: Gotta POST

jdavidb on 2008-05-08T20:10:52

I'm interacting with a .NET (I think) webservice, and I have to create a URL-encoded POST request. Conceptually, no biggie: I've used CGI.pm for years and understand the concept well. (Better than ASP.NET webservices, apparently, as I am aware that semicolon is a valid substitute for ? as a variable separator and ASP.NET webservices are not.) But of course I'm doing this from Java, so there's the extra work of looking up the right library^Wseventeen libraries to use.

Except that the libraries don't seem to fully cover this. Thankfully there's a URLEncoder class, or might just crawl away and die, but there's nothing to create the whole POST request for me from a HashMap or something. I have to generate all the headers myself, including the content-length, and I have to encode each parameter individually and glue them all together myself with = and ;. I mean = and &.

Annoying. So I guess I'll subclass HashMap myself and do it that way. Fun little exercise, I suppose, but this should be there for me.


Not subclassing…

Dom2 on 2008-05-21T07:01:55

Don't use a subclass of HashMap. Write a class that implements Map, and keep a HashMap internally. That'll be much more flexible.

Re:Not subclassing…

jdavidb on 2008-05-21T13:21:45

It's a little late. :)

But really, I don't see how I'm more flexible for implementing Map than simply subclassing HashMap. That just seems like a lot of trouble writing delegates methods that would otherwise just be there if I subclass. It's probably because Perl is the screwdriver with which I attempt to hammer every nail, and the Perl version of hashes as one of the three fundamental data structures is indelibly marked on my mind.

I suppose it makes things easier if ever I want to use some other structure to store the data, but I can't imagine anything better than a hash for storing data. :) This is most likely a limitation on my perspective, but I don't see it. :)