1. 依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
2. 接口
@PostMapping("/webGlLogin")
@ApiOperation(value = "登录")
public ResponseVo webGlLogin(@RequestBody WebGlModel webGlModel) {
return indexService.webGlLogin(webGlModel);
}
3. 逻辑
public ResponseVo webGlLogin(WebGlModel webGlModel) {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// post请求
HttpPost httpPost = new HttpPost("https://www.ilab-x.com/open/api/v2/user/validate?username=" + webGlModel.getUsername()
+ "&password=" + webGlModel.getPassword()
+ "&nonce=" + webGlModel.getNonce()
+ "&cnonce=" + webGlModel.getCnonce()
+ "&appid=" + webGlModel.getAppid()
+ "&signature=" + webGlModel.getSignature());
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
// 响应模型
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
return ResponseVo.success(HttpStatus.SUCCESS, "操作成功", EntityUtils.toString(responseEntity));
}
} catch (Exception e) {
return ResponseVo.error(HttpStatus.ERROR, "请求调用失败", e.getMessage());
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return ResponseVo.error(HttpStatus.ERROR, "服务器异常,请联系管理员", null);
}