通过web.client.RestTemplate在java后端代码中发送https请求
程序员文章站
2022-06-26 14:37:02
...
import org.springframework.web.client.RestTemplate;
一.在外层定义token和url
private String token = “token值”;
private String URL = “xxxxxxxxx”;
二.方法体如下:
public boolean lossReportCard(String workId, String cardNum) {
Integer code;
//添加请求体数据
JSONObject jsonObject = new JSONObject();
jsonObject.put("cusid", workId);
jsonObject.put("bindcardno", cardNum);
try {
//设置请求编码参数
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
headers.add("token", token);
HttpEntity<String> formEntity = new HttpEntity<String>(jsonObject.toString(), headers);
//发送请求
String str = restTemplate.postForEntity(URL, formEntity, String.class).getBody();
JSONObject obj = JSON.parseObject(str);
code = obj.getInteger("code");
} catch (Exception e) {
throw new RenException("上游服务器请求异常");
}
return code == 0;
}
上一篇: 盘点市面上最受关注的10款智能手表