views:

181

answers:

2

Hi,

Sorry, I'm quite new to Java.

I've stumbled across HttpGet and HttpPost which seem to be perfect for my needs, but a little long winded. I have written a rather bad wrapper class, but does anyone know of where to get a better one?

Ideally, I'd be able to do

String response = fetchContent("http://url/", postdata);

where postdata is optional.

Thanks!

+3  A: 

HttpClient sounds like what you want. You certainly can't do stuff like the above in one line, but it's a fully-fledged HTTP library that wraps up Get/Post requests (and the rest).

Brian Agnew
If you end up doing anything real with it, you probably will require authentication/ssl (which HttpClient supports).
Justin
+2  A: 

I would consider using the HttpClient library. From their documentation, you can generate a POST like this:

PostMethod post = new PostMethod("http://jakarata.apache.org/");
NameValuePair[] data = {
  new NameValuePair("user", "joe"),
  new NameValuePair("password", "bloggs")
};
post.setRequestBody(data);
// execute method and handle any error responses.
...
InputStream in = post.getResponseBodyAsStream();
// handle response.

There are a number of advanced options for configuring the client should you eventually required those.

Eric Hauser