org.kuali.ole.rest.OleRestClient Maven / Gradle / Ivy
The newest version!
package org.kuali.ole.rest;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by pvsubrah on 10/2/15.
*/
public class OleRestClient {
public String sendPostRequest(String url, String requestContent) {
String responseContent = null;
try {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Accept", "application/" + "json");
con.setRequestProperty("Content-Type", "application/" + "json");
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(requestContent);
wr.flush();
wr.close();
InputStream resStream = con.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(resStream));
String inputLine = null;
StringBuilder responseContentBuilder = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
responseContentBuilder.append(inputLine);
}
in.close();
responseContent = responseContentBuilder.toString();
} catch (Exception ex) {
ex.printStackTrace();
}
return responseContent;
}
}