使用RestTemplate的POST请求http://www.ousuiot.com:9801/querypubdata 参数 { "signature":"FC73D944C6E54087ABDC3F71AD7857BF", "deviceid":"230417100002" }
时间: 2023-08-24 21:04:34 浏览: 120
基于RestTemplate的使用方法(详解)
可以使用如下代码发送POST请求:
```java
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String, String> requestBody = new HashMap<>();
requestBody.put("signature", "FC73D944C6E54087ABDC3F71AD7857BF");
requestBody.put("deviceid", "230417100002");
HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
String url = "http://www.ousuiot.com:9801/querypubdata";
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
String response = responseEntity.getBody();
```
其中,使用`HttpHeaders`设置请求头为JSON格式,使用`Map`设置请求体参数,并使用`HttpEntity`封装请求体和请求头。最后使用`RestTemplate`发送POST请求并获取响应。
阅读全文