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

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

Related Posts:

  • I'd love some feedback on this entry; feel free to write a piece in the comments. Did you find this interesting? Informative? Confusing? Lacking information, references or sense? I'd like to improve, feedback would help me immensely!
« Trouble with updated iTunes 7
Politikeres synlighet og identitet ved nettdebatt. »

Leave a Reply