title: Automate HTTP POST/GET PHP on file download using header & readfile
body: <p>I developed a PHP script to deliver a file after an authentication.</p>
  
  <pre><code>&lt;?php #SERVER.PHP
  
  if (isset($_REQUEST['uname']) &amp;&amp; isset($_REQUEST['passwd'])) {
  
      if ($_REQUEST['uname']=='a' &amp;&amp; $_REQUEST['passwd']=='a')   {
  
          session_start();
          session_regenerate_id();        
          header('Content-Disposition: attachment; filename=fake_name.pdf');
          readfile('original_name.pdf');        
      }
  }
  ?&gt;
  
  &lt;form name="login" action="test.php" method="get"&gt;
      Username: &lt;input type="text" name="uname"&gt; &lt;br /&gt;
      Password: &lt;input type="text" name="passwd"&gt; &lt;br /&gt;
      &lt;input type="submit" name="submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Hence I want to automate the login and download process, initially I tried using wget to download the file(fake_name.pdf):</p>
  
  <pre><code>$ wget "http://1.1.1.1/server.php?uname=a&amp;passwd=a"
  </code></pre>
  
  <p>but it downloaded a file with content</p>
  
  <pre><code>&lt;form name="login" action="test.php" method="get"&gt;
      Username: &lt;input type="text" name="uname"&gt; &lt;br /&gt;
      Password: &lt;input type="text" name="passwd"&gt; &lt;br /&gt;
      &lt;input type="submit" name="submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I could able to download file "mask_fname.pdf" While accessing from a web browser is working perfectly fine.</p>
  
  <p>Then I tried writing python script to get the file, I am getting only HTML content.</p>
  
  <pre><code>#py1.py
  import httplib, urllib
  params = urllib.urlencode({
      'uname' : 'a',
      'passwd' : 'a'
      })
  headers = {"Content-type": "application/x-www-form-urlencoded",
             "Accept": "text/plain"}
  conn = httplib.HTTPConnection("10.1.1.2:80")
  conn.request("GET", "/mdh/test.php?uname=a&amp;passwd=a",
               params, headers)
  response = conn.getresponse()
  print response.status, response.reason
  data = response.read()
  print data
  conn.close()
  
  #py2.py
  import urllib
  import urllib2
  url = 'http://10.1.1.2/mdh/index.php'
  form_data = {'uname': 'a', 'passwd': 'a'}
  params = urllib.urlencode(form_data)
  response = urllib2.urlopen(url, params)
  data = response.read()
  print data
  </code></pre>
  
  <p>But the output was same for all my tries. Is there any other way to do that. Is there any other approach to automate the download of file(fake_name.pdf) with given username and password, apart from web browser automation(link python::splinter, selenium)?</p>
  
  <p>Ultimately, I need to automate a file download from a server using HTTP with authentication.</p>