title: How do download a file with login using HttpURLConnection
body: <p>For my Android app I need to download a file from a site requiring login. It is not basic http authentication but rather form emulation.</p>
  
  <p>Being new to Android I really don't know how to get started, but in PHP it is easy with cUrl:</p>
  
  <pre><code>// Set standard options
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3");
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_COOKIEJAR, "somecookiejar");
  curl_setopt($ch, CURLOPT_COOKIEFILE, "somecookiefile");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  
  // Login
  curl_setopt($ch, CURLOPT_URL, "https://example.com" );
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
  curl_setopt($ch, CURLOPT_POSTFIELDS, "username=myusername&amp;password=mypassword");
  curl_exec($ch);
  
  // Get the file
  curl_setopt($ch, CURLOPT_URL, "http://example.com/files/myfile.mp3");
  curl_setopt($ch, CURLOPT_POST, 0 );
  writefile(curl_exec($ch), "myfile.mp3");
  curl_close($ch);
  </code></pre>
  
  <p>I would appreciate any help in getting this done in Android.</p>
  
  <p>Note: I mention HttpURLConnection in the subject, but other suggestions are of course more than welcome :)</p>
  
  <p>Edit: I think I should clarify a bit. In the first request to the login form the username and password is set. The server returns a set of cookies which is then handed over to the second request where the actual file download takes place. The code is actually a working example from PHP (albeit anonymized) and my specific problem is handling the cookies between the requests. In cUrl all this cookie stuff happens automatically.
  Thanks!</p>