title: Does Apache HttpClient add the Cookies set by the java.net.CookieHandler to Request?
body: <p>My simple Apache HttpClient (4.0.1) client application makes an HttpGet request to a server URL in the main() method and prints the response. On startup, the application registers an implementation of java.net.CookieHandler in a static block.</p>
  
  <p>On checking the cookies received on the server side, I found that the cookies are not being received by the Server when the HttpClient makes the GET request. </p>
  
  <p>On the other hand, when I replaced the Apache HttpClient with a plain java.net.URL(HTTP_URL).openStream(), the cookies were set by the CookieHandler on the Request and were received by the Server.</p>
  
  <p>Is it that CookieHandler does not work with Apache HttpClient? </p>
  
  <p>Code: </p>
  
  <p><strong>Client.java</strong></p>
  
  <pre>
  static {
          CookieHandler.setDefault(new CookieHandler() {
                  public Map get(URI u, List r) {
                  return Collections.singletonMap("Cookie", 
                                  Collections.singletonList(COOKIE_STRING));
                  }
          });
  }
  </pre>
  
  <p><strong>Using HttpClient</strong> (does not put cookies on request)</p>
  
  <pre>
          HttpClient client = new DefaultHttpClient();
          HttpGet get = new HttpGet(HTTP_URL);
          client.execute(get);
  </pre>
  
  <p><strong>Using java.net.URL</strong> (sets the cookies on request)</p>
  
  <pre>
          URL url = new URL(HTTP_URL); 
          InputStream is = url.openStream();
  </pre>