title: How to handle Cookies with Apache HttpClient 4.3
body: <p>I need to implement a series of HTTP requests in Java and decided to use Apaches HttpClient in version 4.3 (the most current one).</p>
  
  <p>The problem is all these requests use a cookie for session management and I seem to be unable to find a way of accessing that cookie and passing it from request to request. My commands in using curl look something like:</p>
  
  <pre><code># Login
  curl -c cookies -d "UserName=username&amp;Password=password" "https://example.com/Login"
  
  # Upload a file
  curl -b cookies -F fileUpload=@IMG_0013.JPG "https://example.com/File"
  
  # Get results of server processing file
  curl -b cookies "https://example.com/File/1234/Content"
  </code></pre>
  
  <p>They work perfectly. However with HttpClient it seems not to work. What I tried was:</p>
  
  <pre><code>    URI serverAddress = new URI("https://example.com/");
  
      URI loginUri = UriBuilder.fromUri(serverAddress).segment("Login").queryParam("UserName", "username")
              .queryParam("Password", "password").build();
  
      RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();
      CookieStore cookieStore = new BasicCookieStore();
      HttpClientContext context = HttpClientContext.create();
      context.setCookieStore(cookieStore);
  
      CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).setDefaultCookieStore(cookieStore).build();
      HttpGet httpGet = new HttpGet(loginUri);
      CloseableHttpResponse loginResponse = httpClient.execute(httpGet,context);
  
      System.out.println(context.getCookieStore().getCookies());
  </code></pre>
  
  <p>The output of the last line is always an empty list. I think it should contain my Cookie, am I right?</p>
  
  <p>Can someone give me a small example on how to handle the cookie using Apache HttpClient 4.3?</p>
  
  <p>Thanks</p>