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