This article shows a very simple, straightforward example of making an HTTP POST call to a url (http://localhost:8080/movieevents) and sending a JSON payload to that URL.
The REST service invoked in this example is the service published from Java EE as described in this article.
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | package nl.amis.cinema.view; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class CinemaEventGenerator { /* */ private static final String USER_AGENT = "Mozilla/5.0" ; private static final String targeturl = "http://localhost:7101//CinemaMonitor/resources/cinemaevent" ; public static void sendJson(String json) throws MalformedURLException, IOException { //method call for generating json // requestJson = generateJSON(); URL myurl = new URL(targeturl); HttpURLConnection con = (HttpURLConnection)myurl.openConnection(); con.setDoOutput( true ); con.setDoInput( true ); con.setRequestProperty( "Content-Type" , "application/json;" ); con.setRequestProperty( "Accept" , "application/json,text/plain" ); con.setRequestProperty( "Method" , "POST" ); OutputStream os = con.getOutputStream(); os.write(json.toString().getBytes( "UTF-8" )); os.close(); StringBuilder sb = new StringBuilder(); int HttpResult =con.getResponseCode(); if (HttpResult ==HttpURLConnection.HTTP_OK){ BufferedReader br = new BufferedReader( new InputStreamReader(con.getInputStream(), "utf-8" )); String line = null ; while ((line = br.readLine()) != null ) { sb.append(line + "\n" ); } br.close(); System.out.println( "" +sb.toString()); } else { System.out.println(con.getResponseCode()); System.out.println(con.getResponseMessage()); } } public static void main(String[] args) { try { CinemaEventGenerator.sendJson( "{\"room\":\"4\" , \"occupation\":\"5\"}" ); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
In this second example, the service that is being called here is exposed by a PL/SQL procedure as described in this recent article). This example does not use any additional libraries – just the standard Java SE libraries.
package nl.amis.rest;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
public class CinemaEventRouter {
/*
* Thanks to: http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily?rq=1
*/
private static final String USER_AGENT = “Mozilla/5.0”;
private static final String targeturl = “http://localhost:8080/api/movieevents”;
public static void sendPost(String json) {
try {
Map
params.put(“p_json_payload”, json);
StringBuilder postData = new StringBuilder();
for (Map.Entry
if (postData.length() != 0)
postData.append(‘&’);
postData.append(URLEncoder.encode(param.getKey(), “UTF-8”));
postData.append(‘=’);
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), “UTF-8”));
}
byte[] postDataBytes = postData.toString().getBytes(“UTF-8”);
URL url = new URL(targeturl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”);
conn.setRequestProperty(“Content-Length”, String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), “UTF-8”));
for (int c; (c = in.read()) >= 0; System.out.print((char) c))
;
}
catch (Exception e) {
System.out.println(“Call to “+targeturl+” failed.”);
e.printStackTrace();
}
}
}
con.setRequestProperty(“Content-Type”, “application/json;”); this line should be corrected as con.setRequestProperty(“Content-Type”, “application/json”); There’s an additional “;” after “application/json”