title: not able to read browser cookie using HttpClient
body: <p>I need to check browser cookie (along with id - password) to decide if user is valid or not.  I have set cookie in browser when user activates account. At the time of login I need to check for cookie along with credentials. I am using spring security (daoAuthenticationProvider) for authentication.
  I am setting cookie using code : </p>
  
  <pre><code>public void setCookie(String token, HttpServletRequest request, HttpServletResponse response) {
      try {
          Cookie cookie = new Cookie(cookieName, token);
          cookie.setDomain(cookieDomain);
          cookie.setPath(cookiePath);
          cookie.setMaxAge(31536000); // seconds for 365 days
          response.addCookie(cookie);
      } catch(Exception e) {
          log.error("Exception in setting cookie : {}", e.getMessage());
      }   
  }
  </code></pre>
  
  <p>When it's a time to read cookie, I don't have <code>HttpServletRequest</code> object with me. Because it's not a spring controller where I can have request object. So I wrote a method in spring controller which will return the cookie value. Code is : </p>
  
  <pre><code>@RequestMapping(value = "gettoken", method = RequestMethod.GET, produces={"text/html"})
  public ResponseEntity&lt;String&gt; getToken(HttpServletRequest request) {
  
      String token = "";
      Cookie cookies[] = request.getCookies();
  
      if (cookies != null) {
          for (int i = 0; i &lt; cookies.length; i++) {
              System.out.println("Cookie details : " + cookies[i].getName()
              + "\t" + cookies[i].getValue());
              if (cookieName.equalsIgnoreCase(cookies[i].getName())) {
                  token = cookies[i].getValue();
                  break;
              }
          }
      } else {
          logger.info("No cookies found...");
      }
      return new ResponseEntity&lt;String&gt;(token,HttpStatus.OK);
  }
  </code></pre>
  
  <p>This spring controller action returns the cookie value. When I hit this URL, I can see returned cookie value in browser.</p>
  
  <p>Now I call this URL from within the application using HttpClient. Code is : </p>
  
  <pre><code>public boolean isCookieValid(String token) {
      boolean valid = false;
      try {       
          String requestUrl = serverUrl + "gettoken";
          log.info("requestUrl is : {} ", requestUrl);
  
          DefaultHttpClient httpClient = new DefaultHttpClient();
          HttpContext localContext = new BasicHttpContext();
          HttpGet getRequest = new HttpGet(requestUrl.trim());
          getRequest.addHeader("accept", "text/html");
          HttpResponse response = httpClient.execute(getRequest,localContext);
  
          System.out.println("call success : " + response.getStatusLine().getStatusCode());
          if (response.getStatusLine().getStatusCode() != 200) {
              return false;
          }
  
          HttpEntity entity = response.getEntity();
          if (entity != null) {
              String cookieToken = EntityUtils.toString(entity);
              System.out.println("Cookie Token : " + cookieToken);
              if(token.equalsIgnoreCase(cookieToken)) {
                  valid = true;
              }
          }
          httpClient.getConnectionManager().shutdown();      
      } catch (Exception e) {
          log.info("Exception in reading cookies : {}", e.getMessage());      
      }
      return valid;
  }
  </code></pre>
  
  <p>When I hit spring action using http client, I receive HTTP 200 response code. But Spring controller is not able to read cookie. Any idea what could be the problem? Anybody  has better approach than this ?</p>