String url = "http://xxx";
Map<String, Object> params = new HashMap<>();
params.put("app_id", app_id);
params.put("app_token", app_token);
params.put("domain", domain);
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
String resultString = "";
PrintWriter out = null;
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建参数列表
if (params != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : params.keySet()) {
paramList.add(new BasicNameValuePair(key, params.get(key).toString()));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
httpPost.setEntity(entity);
}
// 执行http请求
httpResponse = httpClient.execute(httpPost);
resultString = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
JSONObject json = JSONObject.fromObject(resultString);
out = response.getWriter();
out.append(jsonStr);
out.flush();
} catch (Exception e) {
logger.error("xxx出错", e);
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}