title: VBA WinHTTP to download file from password proteced https website
body: <p>I'm trying to save a file from https password protected site using WinHTTP. Here's the code:</p>
  
  <pre><code>Sub SaveFileFromURL()
  
  Dim FileNum As Long
  Dim FileData() As Byte
  Dim WHTTP As Object
  
  fileUrl = "https://www.website.com/dir1/dir2/file.xls"
  filePath = "C:\myfile.xls"
  
  myuser = "username"
  mypass = "password"
  
  Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
  
  WHTTP.Open "GET", fileUrl, False
  WHTTP.SetCredentials myuser, mypass, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
  WHTTP.Send
  
  FileData = WHTTP.ResponseBody
  Set WHTTP = Nothing
  
  FileNum = FreeFile
  Open filePath For Binary Access Write As #FileNum
      Put #FileNum, 1, FileData
  Close #FileNum
  
  MsgBox "File has been saved!", vbInformation, "Success"
  
  End Sub
  </code></pre>
  
  <p>The problem is with authentication. The file is being saved but when I open it in Excel it's just the html logon page instead of the actual file. If I copy direct file url and paste it into browser addressbar and I'm not logged in to the webpage the effect is the same. I'm presented with the logon page. Then if I enter my login and password the download window will show up allowing me to save the file.</p>
  
  <p>So I think that SetCredentials part of the code is not working properly cause if I debug.print WHTTP.ResponseBody it's html code instead of the acutal file data.</p>
  
  <p>Is there a way to pass userid and password to the WinHTTP so I could be able to properly save the file?</p>
  
  <p>Here's the page address:</p>
  
  <pre><code>https://sst.msde.state.md.us/
  </code></pre>
  
  <p>=======================EDIT:========================</p>
  
  <p>So I've played a little bit with it today and I think I'm moving forward. Here's what I got. I Modyfied the code like this:</p>
  
  <pre><code>Sub SaveFileFromURL()
  
  Dim FileNum As Long
  Dim FileData() As Byte
  Dim WHTTP As Object
  
  fileUrl = "https://www.website.com/dir1/dir2/file.xls"
  filePath = "C:\myfile.xls"
  
  myuser = "username"
  mypass = "password"
  
  strAuthenticate = "start-url=%2F&amp;user=" &amp; myuser &amp; "&amp;password=" &amp; mypass &amp; "&amp;switch=Log+In"
  
  Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
  
  WHTTP.Open "POST", fileUrl, False
  WHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  WHTTP.Send strAuthenticate
  
  WHTTP.Open "GET", fileUrl, False
  WHTTP.Send
  
  Debug.Print WHTTP.GetAllResponseHeaders()
  
  FileData = WHTTP.ResponseBody
  Set WHTTP = Nothing
  
  FileNum = FreeFile
  Open filePath For Binary Access Write As #FileNum
      Put #FileNum, 1, FileData
  Close #FileNum
  
  MsgBox "File has been saved!", vbInformation, "Success"
  
  End Sub
  </code></pre>
  
  <p>When I Debug.Print WHTTP.GetAllResponseHeaders() I get e.g.:</p>
  
  <pre><code>Accept-Ranges: bytes
  Content-Disposition: attachement; filename="xxx"
  Content-Length: xxxxxx
  Content-Type: application/octet-stream
  </code></pre>
  
  <p>So I think that authentication worked but I still cannot save the file. When I continue with:</p>
  
  <pre><code>FileData = WHTTP.ResponseBody
  Set WHTTP = Nothing
  
  FileNum = FreeFile
  Open filePath For Binary Access Write As #FileNum
      Put #FileNum, 1, FileData
  Close #FileNum
  </code></pre>
  
  <p>The content of the saved file is the html webpage itself, but not the file.</p>
  
  <p>Did I do the authentication rigth and the problem is with saving the file to the disk or still is there a problem with authentication and that's why I cannot save it? Any clues?</p>