public static String sendJsonPost(String url,String[] keys,Object[] values)throws Exception{
HttpPost httpPost=null;
CloseableHttpClient client=null;
JSONObject jsonParam=null;
String responseContent=null;
try{
httpPost= new HttpPost(url);
client = HttpClients.createDefault();
jsonParam= new JSONObject();
// jsonParam.put("username","ZMDC");
// jsonParam.put("sms","1440396636364");
// jsonParam.put("sign","48cd217b452cc1df");
if(keys.length>0&&values.length>0&&keys.length==values.length) {
for(int i=0;i<keys.length;i++){
jsonParam.put(keys[i],values[i]);
}
StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpResponse resp = client.execute(httpPost);
if (resp.getStatusLine().getStatusCode() == 200) {
HttpEntity he = resp.getEntity();
responseContent = EntityUtils.toString(he, "UTF-8");
}
}
}catch(Exception e){
}finally {
client.close();
}
return responseContent;
}
Java使用httpclient发送post请求
最新推荐文章于 2024-07-05 18:34:34 发布