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);
About this entry
- You’re currently reading “Specifying what to get, or post,” an entry on journal of tormodh
- Published by: tormodh
- Published at: April 1, 2008 - 10:25
- Category: Java
- Tags: code, get, http, httpclient, Java, parameter, post, request
- Related Posts:
- Java 7 tidbits
- Java Resources
- Generating Class-Path in Jar Manifest with Ant
No comments
Jump to comment form | comments rss | trackback uri