title: xmlHttp to download file
body: <p>Using google chrome i fill in a form and upon pressing the submit button a spreadhsheet automatically starts downloading. I have identified the POST sent to the server but somehow when i send the same request from my script the response is not a file but the original web form. Please see below the request as shown in Chrome :</p>
  
  <hr>
  
  <p>Remote Address: <strong>******</strong> -- Request URL:<strong><em>*</em></strong> -- </p>
  
  <p>Request Method: POST -- Status Code: 200 OK --**</p>
  
  <p>Request Headers</p>
  
  <pre><code>-Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  -Accept-Encoding:gzip,deflate,sdch
  -Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
  -Cache-Control:max-age=0
  -Connection:keep-alive
  -Content-Length:207
  -Content-Type:application/x-www-form-urlencoded
  -Cookie:JSESSIONID=*******; ssoLang=en
  -Host:*******
  -Origin:******
  -Referer:*******
  -User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
  </code></pre>
  
  <p>Query String Parameters</p>
  
  <p>Form Data :  <em>REQUEST</em></p>
  
  <p>Response Headers</p>
  
  <pre><code>-Content-Disposition: filename="xxx.xlsx"
  -Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  -Date:Sat, 31 May 2014 14:28:25 GMT
  -Server:Apache-Coyote/1.1 
  -Transfer-Encoding:chunked 
  </code></pre>
  
  <hr>
  
  <p>The response headers I am getting are different :</p>
  
  <pre><code>-Cache-Control: no-cache, no-store
  -Date: Sat, 31 May 2014 14:55:38 GMT
  -Pragma: no-cache Transfer-Encoding: chunked
  -Content-Type: text/html;charset=UTF-8 Expires: Thu, 01 Jan 1970 00:00:00 GMT
  -Server: Apache-Coyote/1.1
  </code></pre>
  
  <hr>
  
  <p>The script i attempted to do the above is below, can anybody please point out what is wrong here ?</p>
  
  <pre><code>Sub Download_File()
      Dim localFile As String, URL As String, request As String
      localFile = ThisWorkbook.Path &amp; "\test.xlsx"
      URL = ****SENSITIVE***
      Dim XMLreq As Object
      Set XMLreq = CreateObject("MSXML2.XMLHTTP")
  
      request = ****REQUEST*****
  
  
      Dim fileNum As Integer, bytes() As Byte
      With XMLreq
          .Open "POST", URL, False
          .setRequestHeader "Host", ****SENSITIVE***
          .setRequestHeader "Connection", "keep-alive"
          .setRequestHeader "Content-Length", "207"
          .setRequestHeader "Cache-Control", "max-age=0"
          .setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
          .setRequestHeader "Origin", ****SENSITIVE***
          .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36"
          .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
          .setRequestHeader "Referer", ****SENSITIVE***
          .setRequestHeader "Accept-Encoding", "gzip,deflate,sdch"
          .setRequestHeader "Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6"
          .setRequestHeader "Cookie", "JSESSIONID=****SENSITIVE***; ssoLang=en"
          .send (Escape(request))
      While .readyState &lt;&gt; 4: DoEvents: Wend
      If .Status = 200 Then
          Debug.Print .getAllResponseHeaders
          fileNum = FreeFile
          Open localFile For Binary Access Write As #fileNum
          bytes = .responseBody
          Put #fileNum, , bytes
          Close #fileNum
      Else
          MsgBox "XMLhttp POST error " &amp; .statusText &amp; vbCrLf &amp; "Status = " &amp; .Status &amp; vbCrLf &amp; "URL = " &amp; URL
      End If
      End With
  End Sub
  
  
  Private Function Escape(ByVal URL As String) As String
      Dim i As Integer, BadChars As String
      BadChars = "&lt;&gt;%=&amp;!@#£$^()+{[}]|\;:'"",/?"
      For i = 1 To Len(BadChars)
          URL = Replace(URL, Mid(BadChars, i, 1), "%" &amp; Hex(Asc(Mid(BadChars, i, 1))))
      Next i
      URL = Replace(URL, " ", "+")
      Escape = URL
  End Function
  </code></pre>