Package org.glassfish.jersey.examples.httpclient;
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 Apachehttpclientget {public static void main (string[] args) {try {defaulthttpclient
HttpClient = new Defaulthttpclient ();
HttpGet getrequest = new HttpGet (//"http://localhost:8080/RESTfulExample/json/product/get");
"http://10.16.232.79:8555/base/helloworld/h");
Getrequest.addheader ("Accept", "Application/json");
Getrequest.addheader ("Accept", "Text/plain");
HttpResponse response = Httpclient.execute (getrequest); if (Response.getstatusline (). Getstatuscode ()! =) {throw new RuntimeException ("Failed:http Error code: "+ ResPonse.getstatusline (). Getstatuscode ()); } bufferedreader br = new BufferedReader (New InputStreamReader (Response.getentity (). g
Etcontent ())));
String output;
System.out.println ("Output from Server ... \ n");
while (output = Br.readline ()) = null) {System.out.println (output);
} httpclient.getconnectionmanager (). Shutdown ();
} catch (Clientprotocolexception e) {e.printstacktrace ();
} catch (IOException e) {e.printstacktrace (); }
}
}
Post
<pre name= "code" class= "HTML" >package org.glassfish.jersey.examples.httpclient;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.net.MalformedURLException;
Import Java.net.URI;
Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.entity.StringEntity;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.message.BasicHeader;
Import Org.apache.http.protocol.HTTP; public class Apachehttpclientpost {public static void main (string[] args) {Apachehttpclientpost post = new Apachehtt
Pclientpost (); try {//Post.dopost ("Http://localhost:8080/RESTfulExample/json/product/post", "{\" qty\ ": 100,\" name\ ": \" IPad 4\ "}")
;
Post.dopost ("Http://1sss:8555/base/helloworld/post", "{\" qty\ ": 100,\" name\ ": \" IPad 4\ "}"); Post.dopost ("Http://abc/def", "{\" sort\ ": 100,\" name\ ": \" IPaD 4\ "}");
} catch (Exception e) {e.printstacktrace ();
}/*try {defaulthttpclient httpClient = new Defaulthttpclient ();
HttpPost postrequest = new HttpPost ("Http://10.16.232.79:8555/base/helloworld/post");
stringentity input = new Stringentity ("{\" qty\ ": 100,\" name\ ": \" IPad 4\ "}");
Input.setcontenttype ("Application/json");
Postrequest.setentity (input);
HttpResponse response = Httpclient.execute (postrequest);
if (Response.getstatusline (). Getstatuscode ()! = 201) {throw new RuntimeException ("Failed:http error code:")
+ Response.getstatusline (). Getstatuscode ());
} bufferedreader br = new BufferedReader (New InputStreamReader ((Response.getentity (). GetContent ()));
String output;
System.out.println ("Output from Server ... \ n");
while (output = Br.readline ()) = null) {System.out.println (output);
} httpclient.getconnectionmanager (). Shutdown (); } catch (Malformedurlexception e) {E.printstacKtrace ();
} catch (IOException e) {e.printstacktrace (); }*/}//public string DoPost (string Wcfurl,jsonobject jsonobject) throws//Exception {public string DoPost (string
Wcfurl, String jsonobject) throws Exception {HttpClient HttpClient = new Defaulthttpclient ();
HttpResponse response;
HttpPost post = new HttpPost ();
Httpentity httpentity;
Stringentity stringentity = new Stringentity (jsonobject.tostring ()); Stringentity.setcontentencoding (New Basicheader (HTTP.
Content_Type, "Application/json"));
httpentity = stringentity;
Post.setentity (httpentity);
Post.seturi (New URI (Wcfurl));
Post.setheader ("Content-type", "Application/json");
Response = Httpclient.execute (POST);
return Parsehttpresponse (response);
Public String Parsehttpresponse (HttpResponse response) throws Exception {String jsonstring = "";
int status = Response.getstatusline (). Getstatuscode (); if (status = =) {BufferedReader Breader = new BufferedReader (New InputStreamReader (Response.getentity (). GetContent ()));
StringBuffer sb = new StringBuffer ("");
String line = "";
String NL = System.getproperty ("Line.separator");
while (line = Breader.readline ())! = null) {Sb.append (line + NL);
} jsonstring = Sb.tostring ();
Breader.close ();
} return jsonstring; }
}