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.
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.
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.
with all the respect to Apache..
just use HttpClient and PostMethod.
easy as ABC
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();