The post will give two examples to send HTTP POST requests using Java standard API and Apache HttpClient.
1. The example using Java API HttpURLConnection
Send HTTP Post request in Java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package net.tecbar.examples; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpPostExample1 { public String postMessage() throws IOException { final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) Version/7.0.3 Safari/7046A194A"; final StringBuffer content = new StringBuffer(); final String parameters = "?name=tecbar&group=middleschool"; final URL url = new URL("http://tecbar.net/demo/param" + parameters); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", USER_AGENT); conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8"); conn.setDoOutput(true); conn.setConnectTimeout(5000); // Send post request conn.setDoOutput(true); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); final String msg = "<channel>ABC</channel><title>tecbar.net demo pages</title>"; wr.writeBytes(msg); // send request wr.flush(); // close wr.close(); // read response BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String str; while ((str = in.readLine()) != null) { content.append(str); } in.close(); return content.toString(); } public static void main(String[] args) { try { HttpPostExample1 instance = new HttpPostExample1(); String response = instance.postMessage(); System.out.println(response); } catch(IOException ex) { // process the exception } } } |
2. The example using Apache HttpClient (4.x)
Send Http Post request in Java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package net.tecbar.examples; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; public class HttpPostExample2 { private final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) Version/7.0.3 Safari/7046A194A"; public String postMessage() throws IOException { // DefaultHttpClient httpclient = new DefaultHttpClient(); HttpClient httpclient = HttpClientBuilder.create().build(); final String parameters = "?name=tecbar&group=middleschool"; HttpPost httppost = new HttpPost("http://tecbar.net/demo/param" + parameters); httppost.setHeader(HTTP.USER_AGENT, USER_AGENT); httppost.setHeader(HTTP.CONTENT_TYPE, "text/xml; charset=UTF-8"); final String msg = "<channel>ABC</channel><title>tecbar.net demo pages</title>"; httppost.setEntity(new StringEntity(msg)); HttpResponse httpResponse = httpclient.execute(httppost); HttpEntity entity = httpResponse.getEntity(); return EntityUtils.toString(entity); } public static void main(String[] args) { try { HttpPostExample2 instance = new HttpPostExample2(); String response = instance.postMessage(); System.out.println(response); } catch (IOException ex) { // process the exception } } } |