resttemplate 发送带header的post请求

Content-Type:application/json(常用方式,body为json字符串)
    @Autowired
    RestTemplate restTemplate;

    @Test
    public void testPost(){
        //设置请求头参数
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("appId","app_service");

        //设置body参数,并转成json字符串
        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");
        //设置content-type为multipart/form-data
        httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);

        //设置body参数
        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");
        //设置content-type为application/x-www-form-urlencoded
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        //设置body参数
        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

你可能感兴趣的:(http请求)