Java HttpPost request apache based httpclient

<span style="font-family: Arial, Helvetica, sans-serif;">/**</span>
* Send a post request to access the local application and return different results according to the passed parameters
*/
public static void post(String url, List<BasicNameValuePair> formparams) {
// Create a default httpClient instance.
CloseableHttpClient httpclient = HttpClients.createDefault();


// create httppost
HttpPost httppost = new HttpPost(url);
// create parameter queue


UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
System.out.println("executing request" + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("--------------------------------------");
System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
System.out.println("--------------------------------------");
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace ();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace ();
} finally {
// close the connection and release resources
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace ();
}
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813992&siteId=291194637