最近在做一个项目,需要使用RestTemplate发送请求,做一次远程登录,需要拿到登录后的cookie,然后加到请求头上面,再次获取数据,代码如下
Map<String,Object> params=new HashMap<>();
params.put("username",username);
params.put("password",password);
ResponseEntity<String> entity = restTemplate.postForEntity(loginUrl, params, String.class);
List<String> cookies = entity.getHeaders().get("Set-Cookie");
// log.info(message);
HttpHeaders headers = new HttpHeaders();
/* 登录获取Cookie 这里是直接给Cookie,可使用下方的login方法拿到Cookie给入*/
headers.put(HttpHeaders.COOKIE,cookies); //将cookie存入头部
HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
String body = restTemplate.exchange(hostUrl+map.get("yyId"), HttpMethod.GET, requestEntity, String.class).getBody();
log.info(body);
最后
以上就是贤惠草丛最近收集整理的关于使用RestTemplate获取响应头中的cookie,并在请求头加上cookie的全部内容,更多相关使用RestTemplate获取响应头中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复