CONSUMIR SERVICIO WEB REST CON JAVA
package com.prueba;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ConsumirRest {
static String urlGet = "http://m.pension.com.pe:8080/RSAyza/";
static URL url = null;
static HttpURLConnection con = null;
static BufferedReader bufferReader;
static String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) {
//consumirRestGet();
consumirRestPost();
}
public static void consumirRestGet(){
String str = "";
StringBuffer stringBuffer = new StringBuffer();
try {
url = new URL(urlGet + "tipoCondicion.json");
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
bufferReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((str = bufferReader.readLine()) != null) {
stringBuffer.append(str);
stringBuffer.append("\n");
}
System.out.println(stringBuffer.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException ee) {
ee.printStackTrace();
}
}
public static void consumirRestPost(){
try {
url = new URL(urlGet + "usuarios.json");
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("Accept-Charset", "UTF-8");
//String postJsonData = "{\"id\":5,\"countryName\":\"USA\",\"population\":8000}";
String postJsonData = "{\"imei\":\"" + "359771072916946" + "\"}";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(postJsonData.getBytes("UTF-8")); //Para no tener problemas con el UTF-8
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post Data : " + postJsonData);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String output;
StringBuffer response = new StringBuffer();
while ((output = in.readLine()) != null) {
response.append(output);
}
in.close();
System.out.println(response.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.prueba;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ConsumirRest {
static String urlGet = "http://m.pension.com.pe:8080/RSAyza/";
static URL url = null;
static HttpURLConnection con = null;
static BufferedReader bufferReader;
static String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) {
//consumirRestGet();
consumirRestPost();
}
public static void consumirRestGet(){
String str = "";
StringBuffer stringBuffer = new StringBuffer();
try {
url = new URL(urlGet + "tipoCondicion.json");
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
bufferReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((str = bufferReader.readLine()) != null) {
stringBuffer.append(str);
stringBuffer.append("\n");
}
System.out.println(stringBuffer.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException ee) {
ee.printStackTrace();
}
}
public static void consumirRestPost(){
try {
url = new URL(urlGet + "usuarios.json");
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("Accept-Charset", "UTF-8");
//String postJsonData = "{\"id\":5,\"countryName\":\"USA\",\"population\":8000}";
String postJsonData = "{\"imei\":\"" + "359771072916946" + "\"}";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.write(postJsonData.getBytes("UTF-8")); //Para no tener problemas con el UTF-8
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post Data : " + postJsonData);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String output;
StringBuffer response = new StringBuffer();
while ((output = in.readLine()) != null) {
response.append(output);
}
in.close();
System.out.println(response.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Comentarios
Publicar un comentario