title: Download a file with HTTPClient in Java
body: <p>I'm trying to write a java program that logs into a website, types in a search engine, gets the result, and then downloads the excel file that is generated from the results. So far, I can log in ok. and send a search in and get the results. However, I'm having a lot of problems downloading the excel file.</p>
  <p>Looking at the website's source code, I see Ajax and Javascript around the excel file, so I'm assuming it's ajax that helps produce it.</p>
  <pre><code>&lt;input id=&quot;toexcel&quot; type=&quot;image&quot; src=&quot;/websmart/v9.4/XLGP/images/Excel-icon.png&quot; alt=&quot;To Excel&quot; title=&quot;To Excel: Max 20000 Records&quot; onclick=&quot;&quot; /&gt;
  </code></pre>
  <p>The JavaScript part:</p>
  <pre><code>$( document ).ready(function() {
  
          $('#toexcel').click(function(e) { 
              e.preventDefault();
              
              setTask('toexcel');
              
              var ajaxForm = $(&quot;#filter-form&quot;);
                          
              
              $(&quot;.spinner&quot;).show();
  
                  var dataToSend = ajaxForm.serialize();
                  $(&quot;#excelFrame&quot;).attr('src','V7BAE01R.pgm' + '?' + dataToSend);
              setTimeout(function() {
                              $(&quot;.spinner&quot;).hide();
                          }, 5000 );
                  
  </code></pre>
  <p>Using TamperData, when I click the Excel File Export, it sends a post request (which I manage to send in the last part of the code) but I'm not sure where to Get it. I do see in tamperdata the Get that says Application/vnd.ms-excel</p>
  <p><a href="https://i.stack.imgur.com/oHj8L.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oHj8L.jpg" alt="enter image description here" /></a></p>
  <p>I'm not sure what to do to add in the code to get the excel file. Below, I tried to use BufferReader, but it doesn't get my file. Some of the code I simplified because of the Name Value pairs.</p>
  <pre><code>import java.util.List;
  import java.util.ArrayList;
  import org.apache.http.*;
  import java.io.*;
  
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.methods.*;
  import org.apache.http.impl.client.*;
  import org.apache.http.message.*;
  import org.apache.http.util.EntityUtils;
  import org.apache.http.client.entity.*;
  public class httpClientTest {
      
      
      
      public static void main (String[] args) throws ClientProtocolException, IOException {
              //Set up HttpClient
              CloseableHttpClient httpclient = HttpClients.createDefault();
              HttpGet httpGet = new HttpGet(&quot;http://website&quot;);
              CloseableHttpResponse response = httpclient.execute(httpGet);
              
              //Create Post request to log into the AS400 website
              HttpPost httpPost = new HttpPost(&quot;http://loginwebsite&quot;);
              
              List &lt;NameValuePair&gt; nvps = new ArrayList &lt;NameValuePair&gt;();
              
              nvps.add(new BasicNameValuePair(&quot;user&quot;,&quot;username&quot;));
              nvps.add(new BasicNameValuePair(&quot;password&quot;,&quot;password&quot;));
              nvps.add(new BasicNameValuePair(&quot;button&quot;, &quot;Login&quot;));
              nvps.add(new BasicNameValuePair(&quot;task&quot;, &quot;extlogin&quot;));
              httpPost.setEntity(new UrlEncodedFormEntity(nvps));
              response = httpclient.execute(httpPost);
              
              //Get Post response to ensure we logged in, which succeeds
              try{
                  System.out.println(response.getStatusLine());   
                  HttpEntity entity = response.getEntity();
                  EntityUtils.consume(entity);
              } finally{
                  response.close();
              }
              
              //Sent a Post request to filters out recoreds.
              httpPost = new HttpPost(&quot;http://searchresults&quot;);
              nvps.clear();
              nvps.add(new BasicNameValuePair(&quot;ActSts&quot;, &quot;Edit&quot;));
              nvps.add(new BasicNameValuePair(&quot;task&quot;, &quot;filter&quot;));
              nvps.add(new BasicNameValuePair(&quot;Field&quot;, &quot;Plant&quot;));
              response = httpclient.execute(httpPost);
              
              //Displays in printline the html/js of the page. This looks like it DOES display the search results
              //So it IS sending the Post request and receiving a response.
              BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
              String line = &quot;&quot;;
              while ((line = rd.readLine()) != null) {
                  System.out.println(line);
              }
  
          //try to buffer to read in.
          String link = &quot;http://website.com/uri?ActSts=Edit&amp;task=filter&amp;Field=Plant&quot;;
          HttpGet get = new HttpGet(link);
          response = httpclient.execute(get);
          
          InputStream is = response.getEntity().getContent();
          String filePath = &quot;C:\\Users\\WindowsUserName\\Downloads\\WODETAIL_List.xls&quot;;
          FileOutputStream fos = new FileOutputStream(new File(filePath));
          int inByte;
          while((inByte = is.read()) != -1)
              fos.write(inByte);
          is.close();
          fos.close();
  </code></pre>
  <p>I'm pretty sure I'm Posting the data right, but I'm not sure about how to Get the excel file. Could anybody offer some help?</p>
  <p><strong>Edit</strong> I was able to download a file, but it wasn't the excel file. It was a webpage, and I think it's a little bit of an improvement. (Before, nothing downloaded, it just hanged there) The problem was, I think I need to send an authorization key or a cookie with this get request to download the file.</p>
  <p><strong>Edit 2</strong> I've discovered if I just paste to <em><a href="http://website.com/uri?ActSts=Edit&amp;task=filter&amp;Field=Plant" rel="nofollow noreferrer">http://website.com/uri?ActSts=Edit&amp;task=filter&amp;Field=Plant</a></em> in a new tab while logged in, after waiting a little while, I get a link to the excel file. So originally I thought HTTPClient maintains the same cookies throughout as long as the same httpclient is used but apparently it doesn't(?) I guess I have to figure out a way to get a cookie and send it.</p>