HttpClient發送Json數據到指定接口 HttpClient發送Json數據到指定接口

HttpClient發送Json數據到指定接口

https://www.cnblogs.com/fxust/p/8214998.html

/**
* post發送json數據
* @param url
* @param param
* @return
*/
private String doPost(String url, JSONObject param) {
HttpPost httpPost = null;
String result = null;
try {
HttpClient client = new DefaultHttpClient();
httpPost = new HttpPost(url);
if (param != null) {
StringEntity se = new StringEntity(param.toString(), "utf-8");
httpPost.setEntity(se); // post方法中,加入json數據
httpPost.setHeader("Content-Type", "application/json");
}

HttpResponse response = client.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, "utf-8");
}
}

} catch (Exception ex) {
logger.error("發送到接口出錯", ex);
}
return result;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章