/**
* 以post方式调用对方接口方法
* @param pathUrl
*/
public String doPost(String pathUrl, LoginDTO loginDTO){
try {
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("username", loginDTO.getUsername());
paramMap.put("password", loginDTO.getPassword());
paramMap.put("rememberMe", loginDTO.getRememberMe());
paramMap.put("secretKey", loginDTO.getSecretKey());
String json = JSONUtil.toJsonStr(paramMap);
HttpPost httpPost = new HttpPost(pathUrl);
CloseableHttpClient client = HttpClients.createDefault();
StringEntity entity = new StringEntity(json, charset);
entity.setContentEncoding(charset);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode() == 200){
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity, charset);
}
} catch (Exception e) {
//log.error("请求异常",e);
e.printStackTrace();
}
return null;
}
Java对外请求接口
最新推荐文章于 2024-08-05 03:38:56 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)