RestTemplate簡介
RestTemplate對HTTP請求進行了封裝,進行請求的時候可以保留cookie,在下次請求的時候使用;
postForEntity與postForObject功能類似,可以從源碼上面看出異同;
如果想在GET請求的時候帶上cookie,不能使用getForEntity方法,需要使用exchange方法;
代碼示例
import org.springframework.http.*; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.util.List; public class RestRequest { public static void main(String[] args) { List<String> cookies = login(); postForValue(cookies); jsonPost(cookies); getForValue(cookies); getForValueV2(cookies); } // 有些服務器參數需要帶在url上面 private static void getForValueV2(List<String> cookies) { RestTemplate restTemplate = new RestTemplate(); String url = "http://ip:port/xxx?key=value"; HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.put(HttpHeaders.COOKIE, cookies); HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(null, httpHeaders); ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class); String body = responseEntity.getBody(); System.out.println(body); } // 帶cookie的Get請求; private static void getForValue(List<String> cookies) { RestTemplate restTemplate = new RestTemplate(); String url = "http://ip:port/xxx"; MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("key", "value"); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.put(HttpHeaders.COOKIE, cookies); HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, httpHeaders); ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class); String body = responseEntity.getBody(); System.out.println(body); } // json提交請求,帶登陸cookie private static void jsonPost(List<String> cookies) { RestTemplate restTemplate = new RestTemplate(); String url = "http://ip:port/xxx"; String jsonString = "{}"; // json字符串,可以嵌套多級 HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.put(HttpHeaders.COOKIE, cookies); MediaType mediaType = MediaType.parseMediaType("application/json;charset=UTF-8"); httpHeaders.setContentType(mediaType); httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<String> httpEntity = new HttpEntity<>(jsonString, httpHeaders); ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class); String body = responseEntity.getBody(); System.out.println(body); } // post提交請求,帶登陸cookie private static void postForValue(List<String> cookies) { RestTemplate restTemplate = new RestTemplate(); String url = "http://ip:port/xxx"; MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("key", "value"); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.put(HttpHeaders.COOKIE, cookies); HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, httpHeaders); ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class); String body = responseEntity.getBody(); System.out.println(body); } // 登陸請求,請求之後把cookie保留下來 private static List<String> login() { RestTemplate restTemplate = new RestTemplate(); String url = "http://ip:port/xxx"; MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("key", "value"); HttpHeaders httpHeaders = new HttpHeaders(); HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, httpHeaders); ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class); HttpHeaders headers = responseEntity.getHeaders(); return headers.get("Set-Cookie"); } }