简单http使用

CloseableHttpClient httpClient = HttpClients.createDefault();

//http要访问的地址,这里是传入的地址
HttpPost httppost = new HttpPost("http://" + serviceURL + businessURL);
UrlEncodedFormEntity uefEntity;
CloseableHttpResponse response = null;
try {

//对要传递的数据进行编码
uefEntity = new UrlEncodedFormEntity(params, "UTF-8");

//将数据放置到httppost中一起传递到要访问的位置
httppost.setEntity(uefEntity);

//这一步就是访问
response = httpClient.execute(httppost);

//这是访问后传回的数据,我这里传回的是状态和信息
HttpEntity enti = response.getEntity();
if (enti != null) {

对数据的处理
JSONObject result = JSONObject.fromObject(EntityUtils.toString(enti, "UTF-8"));
@SuppressWarnings("unchecked")
HashMap<String,String> map = (HashMap<String,String>) JSONObject.toBean(result,HashMap.class);

//判断是否访问成功
if (map.get("status").equals("success")) {
return true;
}
}

 

posted @ 2018-03-15 14:41  静静的代码生活  阅读(275)  评论(0编辑  收藏  举报