通过HttpPost 以body传参方式调用接口
##解决中文乱码问题
url:http://www.baidu.com/getContent
//将需要的body参数添加到map中
xmlStr:`<?xml version="1.0" encoding="UTF-8"?><APPROVEDATAINFO>
<SBLSH>12345678</SBLSH>
<ZZZZZ>370000000000</ZZZZZ>
<EXPRESSTYPE>解决中文乱码</EXPRESSTYPE>
<TIME>20230509181354</TIME>
</APPROVEDATAINFO>`
Map<String, String> requestParams = new HashMap<String,String>();
requestParams.put("accessToken", token);
requestParams.put("xmlStr", xmlStr);
public String httpURLConnectionPOST (String url, Map<String, String> requestParams) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : requestParams.entrySet()) {
params.add(new BasicNameValuePair((String) entry.getKey(),
(String) entry.getValue()));
}
HttpPost post = new HttpPost(url);
//添加header参数
post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
post.setHeader("kkk", "123");
post.setHeader("xxx", "456");
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf-8");
post.setEntity(formEntity);
CloseableHttpResponse response = httpClient.execute(post);
HttpEntity resEntity = response.getEntity();
String res = EntityUtils.toString(resEntity, "utf-8");
return res;
}