Spring boot 优雅进行网络请求

2022-02-10 18:40:35 浏览数 (1)

POST

代码语言:javascript复制
RestTemplate restTemplate = new RestTemplate();
String url = "https://clearseve.com";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("email", "clearseve@163.com");

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map,headers);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
System.out.println(response.getBody());

GET

代码语言:javascript复制
RestTemplate restTemplate = new RestTemplate();
String url = "https://graph.qq.com/oauth2.0/token";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("grant_type", "authorization_code");
map.add("client_id", "101838604");
map.add("client_secret", "787239b197160bd782f77876db328c42");
map.add("code", "B34885DEB7A0D7AE014DDEB53C9CB401");
map.add("code", "B34885DEB7A0D7AE014DDEB53C9CB401");
map.add("redirect_uri", "http://localhost:6602/callback/qq");

ResponseEntity<String> response = restTemplate.getForEntity(url, String.class, map);
System.out.println(response.getBody());

0 人点赞