In this tutorials we are going to see the how to access the RESTful web service using Apache HttpClient.
Here i am going to create two clients to access GET and POST request
Add httpcore-4.1.jar, httpclient-4.1.1.jar, commons-logging-1.1.1.jar in you build path
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
RSETApacheHttpClient.java
Now run the client program see the following output in console
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
RSETApacheHttpClient.java
Now run the client program see the following output in console
Here i am going to create two clients to access GET and POST request
Add httpcore-4.1.jar, httpclient-4.1.1.jar, commons-logging-1.1.1.jar in you build path
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
RSETApacheHttpClient.java
package com.javatutorialscorner.jaxrs.client; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; public class RSETApacheHttpClient { public static void main(String[] args) { try { DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet( "http://localhost:8080/RESTful-WebService/rest/getservice/javatutorialscorner.com"); HttpResponse httpResponse = defaultHttpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed to connect service" + httpResponse.getStatusLine().getStatusCode()); } else { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(httpResponse.getEntity() .getContent())); String response = null; while ((response = bufferedReader.readLine()) != null) { System.out.println(response); } } defaultHttpClient.getConnectionManager().shutdown(); } catch (ClientProtocolException 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
POST Request
Web Site : javatutorialscorner.com
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
RSETApacheHttpClient.java
package com.javatutorialscorner.jaxrs.client; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.entity.StringEntity; public class RSETApacheHttpClient { public static void main(String[] args) { try { DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost( "http://localhost:8080/RESTful-WebService/rest/restfulpost/post"); StringEntity json = new StringEntity( "{\"name\":\"Ram\",\"id\":33,\"department\":\"EEE\",\"year\":2011}"); json.setContentType("application/json"); httpPost.setEntity(json); HttpResponse httpResponse = defaultHttpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() != 201) { throw new RuntimeException("Failed to connect service" + httpResponse.getStatusLine().getStatusCode()); } else { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(httpResponse.getEntity() .getContent())); String response = null; while ((response = bufferedReader.readLine()) != null) { System.out.println(response); } } defaultHttpClient.getConnectionManager().shutdown(); } catch (UnsupportedEncodingException 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