title: http request to download a file in java
body: <p>Im really new to Java so i hope you can help me.
  I want to Programm a http request to download a File (pdf) from a Website. 
  The Website is using cookies. So i have to send a first request to get a cookie and then a second to download the file. </p>
  
  <p>I want to use no external Libraries if possible. </p>
  
  <p>This is what i gotso far:</p>
  
  <pre><code>URLConnection request = null;
      String url = str;
      CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
      request = new URL(url ).openConnection();
  
      System.setProperty("http.maxRedirects", "100");
  
      InputStream in = request.getInputStream();
      File downloadedFile = File.createTempFile("test", ".pdf");
      FileOutputStream out = new FileOutputStream(downloadedFile);        
      byte[] buffer = new byte[1024];
      int len = in.read(buffer);
      while (len != -1) {
          out.write(buffer, 0, len);
          len = in.read(buffer);
          if (Thread.interrupted()) {
              try {
                  throw new InterruptedException();
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
      in.close();
      out.close();
      return downloadedFile.getAbsolutePath();
  </code></pre>
  
  <p>my code seems to do nothing...</p>
  
  <p>Best whishes M1K</p>