1 post 参数传输类型: url编码格式 application/x-www-form-urlencoded
/**
* 更加简便的写法
* post 参数传输类型: url编码格式 application/x-www-form-urlencoded
*/
@Test
public void test() throws URISyntaxException, UnsupportedEncodingException {
// 设置 form 格式的参数,如果 contentType 选择了 application/x-www-form-urlencoded,则必须用 MultiValueMap 进行参数的封装
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap(){{
add("appId", "1000000");
}};
paramMap.add("sign", DigestUtils.md5DigestAsHex(String.valueOf(paramMap.get("appId").get(0)).getBytes(StandardCharsets.UTF_8)));
// 封装 request 请求的 各种Header和参数
RequestEntity requestEntity = RequestEntity
.post(new URI("")) // 可以不填
.contentType(MediaType.APPLICATION_FORM_URLENCODED) // 参数传输类型 url编码格式 application/x-www-form-urlencoded
.accept(MediaType.ALL).acceptCharset(StandardCharsets.UTF_8)
.body(paramMap);
ResponseEntity<String> result = rest.postForEntity("http://www.baidu.com", requestEntity, String.class);
System.out.println(result);
}