Home > Back-end >  What's the main problem HTTP POST way to write this way?
What's the main problem HTTP POST way to write this way?

Time:11-26

http://xx.xx.xx.xx:xxxx/aa? The Body=MSG

String httpRes="";//get the results
CloseableHttpClient httpClient=null;
CloseableHttpResponse response=null;

The String url="http://xx.x.x:x/aa";

String MSG=""

Try {
HttpClient=HttpClients. CreateDefault ();
HttpPost post=new HttpPost (url);

RequestConfig RequestConfig=RequestConfig. Custom (.) setSocketTimeout (6000). SetConnectTimeout (3000). The build ();
Post. SetConfig (requestConfig);

String Body=URLEncoder. Encode (MSG, "utf-8");

//build content
List FormParams=new ArrayList (a);
FormParams. Add (new BasicNameValuePair (" Body ", the Body));
HttpEntity entity=new UrlEncodedFormEntity (formParams, "utf-8");

//send data request
Post. AddHeader (" the content-type ", "application/x - WWW - form - urlencoded; charset=UTF-8");
Post. SetEntity (entity);
Try {
The response=httpClient. Execute (post);
//removed from the response object HttpEntity
HttpEntity entity1=response. GetEntity ();
HttpRes=EntityUtils. ToString (entity1, "utf-8");

EntityUtils. Consume (response. GetEntity ());//completely consumption
} the catch (IOException e) {
e.printStackTrace();
} the finally {
Try {
if (null !=response)
The response. The close ();
} the catch (IOException e) {
e.printStackTrace();
}
}
} the catch (Exception e) {
e.printStackTrace();
} the finally {
//release link
Try {
HttpClient. Close ();
} the catch (Exception e) {
e.printStackTrace();
}
}

Return httpRes;

  • Related