Thursday 24, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
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.javatutorialcorner.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.javatutorialcorner.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

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • 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 cr… Read More
  • JAX-WS Hello World Web service – Document StyleIn this tutorial we are going to see how to create web service end point and client in document style SAOP binding . Follow the steps given below to create SOAP web service. 1. Open Eclipse, Select File –> New  --&… Read More
  • RESTful Web service with Jersey Client In this tutorials we are going to see the how to access the RESTful web service using jersey client API. Here i am going to create two clients to access GET and POST request . Add jersey-client-1.17.1.jar  in build pat… Read More
  • JAX-WS wsimport tool In this tutorial we are going to see how to generate client using wsimport tool The wsimpot tool is used to generate generate web service client from WSDL file to access the published web service. you can see the wsimport to… Read More
  • JAX-RS @GET using RESTful webserviceIn this tutorial we are going to see  about RESTful web service’s @GET annotation1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called RESTful-WebService3. … Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: RESTful Java Client using HttpURLConnection Rating: 5 Reviewed By: eHowToNow