0

What is most easiest and convenient way to do a POST request to remote host programmatically using Java EE?

I need to POST some parameters and then read values of returned response parameters.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
adrift
  • 637
  • 2
  • 10
  • 34

4 Answers4

1

I would use Apache HttpComponents. It's a well-documented reliable library for doing HTTP interaction.

I would shy away from trying to write anything yourself via the basic Java SE APIs. For anything other than the simplest scenario you're going to find you're doing a lot of work.

Check out the examples documented here - in particular the POST example.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

Definitely Apache Http Client! http://hc.apache.org/httpclient-3.x/

Elchin
  • 584
  • 6
  • 19
0

with all the respect to Apache..
just use HttpClient and PostMethod.

easy as ABC

socksocket
  • 4,271
  • 11
  • 45
  • 70
0

Just open connection to your url and post your data.

URL wsurl = new URL(url);
URLConnection conn = wsurl.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
StringBuilder output = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
   output.append(line);
}
wr.close();
rd.close();
roel
  • 2,005
  • 3
  • 26
  • 41