Public static String doPost (String URL, string encoding, String contentType, String senddata)
throws Exception {
httpclientbuilder httpclientbuilder = Httpclientbuilder.create ();
closeablehttpclient httpclient = httpclientbuilder.build ();
httppost httppost = new HttpPost (URL);
stringentity myentity = new Stringentity (senddata, encoding);
Myentity.setcontenttype (contentType);
httppost. setentity (myentity);
HttpResponse response = Httpclient.execute (httppost);
httpentity resentity = response.getentity ();
inputstreamreader reader = new InputStreamReader (resentity.getcontent (), encoding);
char[] buff = new char['? ');
StringBuilder sb = new StringBuilder ();
int length;
While (length = reader.read (buff))! =-1) {
sb.append (new String (buff, 0, length));
}
httpclient.close ();
return sb.tostring ();
}
Public static void Requestget (String urlwithparams) throws Exception {
closeablehttpclient httpclient = httpclientbuilder.create (). build ();
//httpget httpget = new HttpGet ("http://www.baidu.com/");
httpget httpget = new HttpGet (urlwithparams);
//Configuration Request time-out setting
requestconfig requestconfig = Requestconfig.custom (). Setconnectionrequesttimeout (5000). Setconnecttimeout (The)
. SetSocketTimeout. Build ();
httpget. Setconfig (Requestconfig);
closeablehttpresponse response = Httpclient.execute (httpget);
System. out.println ("StatusCode" + response.getstatusline (). Getstatuscode ());
httpentity entity = response.getentity ();
String jsonstr = entityutils.tostring (entity); , "UTF-8");
System. out.println (jsonstr);
httpget.releaseconnection ();
}
HttpClient 4.x Post Get method