天天看點

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