springboot 发送post请求
springboot 发送post请求import org.springframework.util.MultiValueMap;import org.springframework.web.client.RestTemplate;public static String sendPOSTRequest(String url, MultiValueMap<String, String>
·
springboot 发送post请求
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public static String sendPOSTRequest(String url, MultiValueMap<String, String> params) {
RestTemplate client = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
HttpMethod method = HttpMethod.POST;
// 以表单的方式提交
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// 将请求头部和参数合成一个请求
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
// 执行HTTP请求,将返回的结构使用String类格式化
ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
return response.getBody();
}
String json = this.sendPOSTRequest(dataSyncConfig.getUrl(), null);
更多推荐
已为社区贡献1条内容
所有评论(0)