Content-Type:application/json(常用方式,body为json字符串)
@Autowired
RestTemplate restTemplate;
@Test
public void testPost(){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("appId","app_service");
BasicDownloadForm basicDownloadForm = new BasicDownloadForm();
basicDownloadForm.setOrgCode("900000");
basicDownloadForm.setDataType("INTERCEPT");
basicDownloadForm.setPdaCode("JSYG22200110001");
basicDownloadForm.setVersionNo("1573285609000");
String content = JSON.toJSONString(basicDownloadForm);
HttpEntity<String> httpEntity = new HttpEntity<>(content, httpHeaders);
String response = restTemplate.postForObject("http://xxxxxx.xxx.xx/xxxxxxx/xxxxx/xxxxxxxxxxxx", httpEntity, String.class);
System.out.println(response);
}
Content-Type:multipart/form-data
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("appId","app_service");
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("orgCode","900000");
multiValueMap.add("dataType","INTERCEPT");
multiValueMap.add("pdaCode","JSYG22200110001");
multiValueMap.add("versionNo","1573285609000");
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(multiValueMap, httpHeaders);
String response = restTemplate.postForObject("http://xxxxxx.xxx.xx/xxxxxxx/xxxxx/xxxxxxxxxxxx", httpEntity, String.class);
System.out.println(response);
form-data的body格式
--krkRYHbkchl5hd63CfwpJwpHdpo-ivxjrlis2Vb--
Content-Disposition: form-data; name="orgCode"
Content-Type: text/plain;charset=UTF-8
Content-Length: 6
900000
--krkRYHbkchl5hd63CfwpJwpHdpo-ivxjrlis2Vb
Content-Disposition: form-data; name="dataType"
Content-Type: text/plain;charset=UTF-8
Content-Length: 9
INTERCEPT
--krkRYHbkchl5hd63CfwpJwpHdpo-ivxjrlis2Vb
Content-Disposition: form-data; name="pdaCode"
Content-Type: text/plain;charset=UTF-8
Content-Length: 15
JSYG22200110001
--krkRYHbkchl5hd63CfwpJwpHdpo-ivxjrlis2Vb
Content-Disposition: form-data; name="versionNo"
Content-Type: text/plain;charset=UTF-8
Content-Length: 13
1573285609000
--krkRYHbkchl5hd63CfwpJwpHdpo-ivxjrlis2Vb--
Content-Type:application/x-www-form-urlencoded
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("appId","app_service");
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("orgCode","900000");
multiValueMap.add("dataType","INTERCEPT");
multiValueMap.add("pdaCode","JSYG22200110001");
multiValueMap.add("versionNo","1573285609000");
HttpEntity httpEntity = new HttpEntity(multiValueMap, httpHeaders);
String response = restTemplate.postForObject("http://xxxxxx.xxx.xx/xxxxxxx/xxxxx/xxxxxxxxxxxx", httpEntity, String.class);
System.out.println(response);
x-www-form-urlencoded 的body格式
orgCode=900000&dataType=INTERCEPT&pdaCode=JSYG22200110001&versionNo=1573285609000