Specifying what to get, or post

I’ve noticed that I generally don’t post too much on this blog/journal/whatever. This partly comes from the fact that I suspect everyone to already know what I do, or find out. Thus I avoid public ridicule if I don’t write anything.

Well, as I really know that everybody can’t possibly know everything, I’ll try and just post stuff as I come across it, even if it does look simple from time to time. I found out yesterday that I do know simple stuff that others don’t - so here goes.

Whenever you just want to do a HTTP GET method to fetch something in Java, it might not be the easiest thing to do. Or perhaps it is? You could screw around with java.net.URLConnection, opening connections, fighting error handling, writing to the connection’s stream in case of POST calls… or you could get Apache’s HttpClient. The call would then be:

GetMethod getMethod = new GetMethod("http://www.google.com/search");
NameValuePair[] parameters = {
	New NameValuePair("q","searchstring")
};
getMethod.setQueryString(parameters);

Post requests works the same way, just create a PostMethod and instead do a:

postMethod.setRequestBody(parameters);

« Trouble with updated iTunes 7
Politikeres synlighet og identitet ved nettdebatt. »