httpPost方式訪問url


public static String sendHttpPostIns(String url, String param) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
try {
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new StringEntity(param));
//httpPost.setEntity(new StringEntity(param,"utf-8"));
httpPost.addHeader("Content-type","application/json; charset=utf-8");
httpPost.setHeader("Accept", "application/json");

HttpEntity entity = httpclient.execute(httpPost).getEntity();

if (entity == null) {
return null;
}
String data = EntityUtils.toString(entity, Consts.UTF_8);
if (StringUtil.isEmpty(data)) {
return null;
}
return data;
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}finally {
if(httpResponse!=null){
httpResponse.close();
}
if(httpclient!=null){
httpclient.close();
}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM