title: How to access / save cookies in Java
body: <p>I am writing some software in Java.</p>
  
  <p>I am testing the cookie managment functionality but I can't seem to get cookies working. I wrote a quick PHP that drops a cookie, and I tested that in my browser and the cookie drops fine.</p>
  
  <p>However, take a look at this code:</p>
  
  <pre><code>    CookieManager cManager = new CookieManager();
      CookieHandler.setDefault(cManager);
      try
      {
  
          URL url = new URL("http://localhost/_techfactory/apage.php");
          URLConnection connection = url.openConnection();
          CookieStore cookieJar = cManager.getCookieStore();
          List&lt;HttpCookie&gt; cookies = cookieJar.getCookies();
          for ( HttpCookie cookie : cookies)
          {
              System.out.print(cookie);
          }
          Boolean isittrue = cookies.isEmpty();
          System.out.print(isittrue);
          BufferedReader bin = new BufferedReader ( new InputStreamReader(connection.getInputStream()));
          String line;
          while ( ( line = bin.readLine()) != null )  
          System.out.print(line);
  
      }
      catch ( Exception e )
      {
          System.out.println(e);
      }
  </code></pre>
  
  <p>This, although being syntactically correct, doesn't output anything, except the HTML from the page. Now by all accounts, the CookieManager implementaiton is a concrete implementation of CookieHandler, CookiePolicy and CookieStore. However, it just refuses to work for me, what I am I doing wrong?</p>