Consuming GET and POST request rest webservices in cq5
Hi All,
http://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/
Here is my simple POST request webservices with header and body
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 NetClientPost {
// http://localhost:8080/RESTfulExample/json/product/post
public static void main(String[] args) {
try {
//URL url = new URL("http://api.sewadwaar.rajasthan.gov.in/app/live/evolt_services?client_id=4ec78278-dfa0-4d26-9cd6-c273099f8afa");
URL url = new URL("http://rsldc.rajasthan.gov.in/rsldcscripts/ASBIViewRest.dll/datasnap/rest/TASBIViewREST/getiview");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml");
conn.setRequestProperty("Content-Type", "application/json");
//conn.setRequestProperty("SOAPAction", "http://tempuri.org/EvoltServices");
//String input = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tem='http://tempuri.org/'><soapenv:Header/><soapenv:Body><tem:EvoltServices/></soapenv:Body></soapenv:Envelope>";
String input = "{\"_parameters\": [{\"getiview\": {\"name\": \"mobiledm\",\"axpapp\": \"rsldcnew\",\"username\":\"mobileuser\",\"password\":\"f42072a505a77871369cbfcade976a3a\",\"seed\":\"\",\"s\": \"\",\"pageno\": \"1\",\"pagesize\": \"100\",\"sqlpagination\": \"true\",\"params\": {}}}]}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
//throw new RuntimeException("Failed : HTTP error code : "
// + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and here is my simple GET request webservices
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class NetClientGet {
// http://localhost:8080/RESTfulExample/json/product/get
public static void main(String[] args) {
try {
URL url = new URL("http://164.100.222.109/eemsapptest/webservices/eemsservice.asmx/GetCasteCategory");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}