title: Spring RestTemplate session
body: <p>I'm trying to use spring rest template to do a post request to login in.</p>
  
  <p>When I receive the response in my first request i store my session id which is received via cookie. I retrieve it in a set-cookie response header which i get via:</p>
  
  <pre><code>    //first request
  RestTemplate restTemplate = new RestTemplate();
  
  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
  
  LinkedMultiValueMap&lt;String, Object&gt; mvm = new LinkedMultiValueMap&lt;String, Object&gt;();
  mvm.add("LoginForm_Login", "login");
  mvm.add("LoginForm_Password", "password");
  
  ResponseEntity&lt;String&gt; result = restTemplate.exchange(uriDWLogin, HttpMethod.POST, requestEntity, String.class);
  
  result.getHeaders().get("Set-Cookie").stream().forEach(System.out::println);
  </code></pre>
  
  <p>then in every subsequent request i set the Cookie request header with the values received in the first request:</p>
  
  <pre><code>//subsequent request
  RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders();
  headers.set("Cookie",cookies.stream().collect(Collectors.joining(";")));
  HttpEntity&lt;String&gt; entity = new HttpEntity&lt;String&gt;(headers);
  RestTemplate.exchange("http://url", HttpMethod.POST, entity, String.class);
  </code></pre>
  
  <p>all goes well for the second request, but I can not keep the session for the others requests</p>