Java client for restful web service using java.net package
In this page you will come to know how to create java client for restful web services using java.net package. We will have
two sections here, the first section talks about how to connect to "GET" request, and the second section shows how to connect to "POST"
type of requests.
We are referring to the previous example as a restful web services.
Java Client for GET Request
package com.java2novice.rest.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class RestJavaNetClient {
public static void main(String a[]){
String url = "http://localhost:8080/RestfulWebServices/order-inventory/order/1016";
HttpURLConnection urlConn = null;
BufferedReader reader = null;
try {
URL urlObj = new URL(url);
urlConn = (HttpURLConnection) urlObj.openConnection();
urlConn.setRequestMethod("GET");
urlConn.setConnectTimeout(5000);
urlConn.setReadTimeout(5000);
urlConn.setRequestProperty("Accept", "application/json");
if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.err.println("Unable to connect to the URL...");
return;
}
System.out.println("Connected to the server...");
InputStream is = urlConn.getInputStream();
reader = new BufferedReader(new InputStreamReader((is)));
System.out.println("Reading data from server...");
String tmpStr = null;
while((tmpStr = reader.readLine()) != null){
System.out.println(tmpStr);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(reader != null) reader.close();
if(urlConn != null) urlConn.disconnect();
} catch(Exception ex){
}
}
}
}
|
The output for above code is:
Output: |
Connected to the server...
Reading data from server...
{"custmer":"Java2Novice","address":"Bangalore","bill-amount":"$2000"}
|
Java Client for POST Request
In this example you will see how to send json input through java client using POST method:
package com.java2novice.rest.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class PostJavaNetClient {
public static void main(String a[]){
String url = "http://localhost:8080/RestfulWebServices/order-inventory/order";
HttpURLConnection urlConn = null;
BufferedReader reader = null;
OutputStream ouputStream = null;
String jsonInput = "{\"custmer\":\"Java2novice\",\"address\":\"Bangalore\","+
"\"bill-amount\":\"$2000\"}";
try {
URL urlObj = new URL(url);
urlConn = (HttpURLConnection) urlObj.openConnection();
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");
urlConn.setRequestProperty("Content-Type", "application/json");
urlConn.setConnectTimeout(5000);
urlConn.setReadTimeout(5000);
urlConn.setRequestProperty("Accept", "application/json");
// send json input request
ouputStream = urlConn.getOutputStream();
ouputStream.write(jsonInput.getBytes());
ouputStream.flush();
if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.err.println("Unable to connect to the URL...");
return;
}
System.out.println("Connected to the server...");
InputStream is = urlConn.getInputStream();
reader = new BufferedReader(new InputStreamReader((is)));
String tmpStr = null;
while((tmpStr = reader.readLine()) != null){
System.out.println(tmpStr);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(reader != null) reader.close();
if(urlConn != null) urlConn.disconnect();
} catch(Exception ex){
}
}
}
}
|
The output for above code is:
Output: |
Connected to the server...
Your order is in-progress
|
|