1. 程式人生 > >restTemplate請求資源時 設定請求頭

restTemplate請求資源時 設定請求頭

1.     HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("key1", "values");
        requestHeaders.add("key2", "ddd");
2.     HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> response = template.exchange(url, HttpMethod.GET, requestEntity, String.class, params);

        String sttr = response.getBody();

第一步:定義HttpHeaders,然後設定header裡要帶的資訊;

第二步:把設定好的header放到HttpEntity<String>裡;

第三步:HttpEntity<String>放到exchange的引數裡;

第四步:getBody()取結果;