public void httpDoGet(String url, Map<String, Object> params) {
//获取默认Client实例
CloseableHttpClient client = HttpClients.createDefault();
//拼接Get请求Url参数
String apiUrl = url;
StringBuffer sb =new StringBuffer();
int i =0;
for (String key : params.keySet()) {
if (i ==0) {
sb.append("?");
} else {
sb.append("&");
}
sb.append(key).append("=").append(params.get(key));
i++;
}
apiUrl += sb;
//创建HttpGet实例
HttpGet httpGet =new HttpGet(apiUrl);
//添加请求头信息
httpGet.addHeader("Content-Type", "application/json;charset=utf-8");
try {
//获取响应的所有内容
CloseableHttpResponse response = client.execute(httpGet);
try {
//获取响应消息体内容
HttpEntity entity = response.getEntity();
//转成String+防止乱码
String result = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
response.close();
}
} catch (IOException e) {
new RuntimeException(e);
} finally {
client.close();
}
}