Spring RestTemplate用法 Post Get Cookie

2021-03-22 07:28

阅读:332

标签:get请求   ade   源码   封装   cep   charset   lis   link   org   

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 cookies = login();

        postForValue(cookies);

        jsonPost(cookies);

        getForValue(cookies);
    }

    // 带cookie的Get请求;
    private static void getForValue(List cookies) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://ip:port/xxx";
        MultiValueMap params = new LinkedMultiValueMap();
        params.add("key", "value");
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(HttpHeaders.COOKIE, cookies);
        HttpEntity> httpEntity = new HttpEntity(params, httpHeaders);
        ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
        String body = responseEntity.getBody();
        System.out.println(body);
    }

    // json提交请求,带登陆cookie
    private static void jsonPost(List 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 httpEntity = new HttpEntity(jsonString, httpHeaders);
        ResponseEntity responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        String body = responseEntity.getBody();
        System.out.println(body);
    }

    // post提交请求,带登陆cookie
    private static void postForValue(List cookies) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://ip:port/xxx";
        MultiValueMap params = new LinkedMultiValueMap();
        params.add("key", "value");
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(HttpHeaders.COOKIE, cookies);
        HttpEntity> httpEntity = new HttpEntity(params, httpHeaders);
        ResponseEntity responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        String body = responseEntity.getBody();
        System.out.println(body);
    }

    // 登陆请求,请求之后把cookie保留下来
    private static List login() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://ip:port/xxx";
        MultiValueMap params = new LinkedMultiValueMap();
        params.add("key", "value");
        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity> httpEntity = new HttpEntity(params, httpHeaders);
        ResponseEntity responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        HttpHeaders headers = responseEntity.getHeaders();
        return headers.get("Set-Cookie");
    }
}

 

Spring RestTemplate用法 Post Get Cookie

标签:get请求   ade   源码   封装   cep   charset   lis   link   org   

原文地址:https://www.cnblogs.com/stono/p/13889536.html


评论


亲,登录后才可以留言!