Saturday, 4 January 2014

RESTful Java Client using HttpURLConnection

In this tutorials we are going to see the how to access the RESTful web service using Java built-in client library .
Here i am going to create two clients to access GET and POST request
GET Request
Refer this example for creating service http://www.javatutorialscorner.com/2014/01/jax-rs-get-using-restful-webservice.html
After create and run the service then create java client  as given below
RSETJavaClient.java
package com.javatutorialscorner.jaxrs.client;

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 RSETJavaClient {
public static void main(String[] args) {
URL url;
try {
url = new URL(
"http://localhost:8080/RESTful-WebService/rest/getservice/javatutorialscorner.com");
HttpURLConnection httpsURLConnection = (HttpURLConnection) url
.openConnection();
httpsURLConnection.setRequestMethod("GET");
if (httpsURLConnection.getResponseCode() == 200) {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(
httpsURLConnection.getInputStream()));
String response = null;
while ((response = bufferedReader.readLine()) != null) {
System.out.println(response);
}
} else {
throw new RuntimeException("Failed to connect service"
+ httpsURLConnection.getResponseCode());
}
httpsURLConnection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Now run the client program see the following output in console

Web Site : javatutorialscorner.com

POST Request

Refer this example for creating service http://www.javatutorialscorner.com/2014/01/jax-rx-post-using-restful-web-service.html

After create and run the service then create java client  as given below

 RSETJavaClient.java

package com.javatutorialscorner.jaxrs.client;

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 RSETJavaClient {
public static void main(String[] args) {
URL url;
try {
url = new URL(
"http://localhost:8080/RESTful-WebService/rest/restfulpost/post");
HttpURLConnection httpsURLConnection = (HttpURLConnection) url
.openConnection();
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setRequestMethod("POST");
httpsURLConnection.addRequestProperty("Content-Type",
"application/json");
String json = "{\"name\":\"Ram\",\"id\":33,\"department\":\"EEE\",\"year\":2011}";
OutputStream outputStream = httpsURLConnection.getOutputStream();
outputStream.write(json.getBytes());
outputStream.flush();
// outputStream.close();

if (httpsURLConnection.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed to connect service"
+ httpsURLConnection.getResponseCode());
} else {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(
httpsURLConnection.getInputStream()));
String response = null;
while ((response = bufferedReader.readLine()) != null) {
System.out.println(response);
}

}
httpsURLConnection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Now run the client program see the following output in console

Data Received Successfuly : Ram

0 comments

Post a Comment