Search code examples
vbscriptxmlhttprequest

Download zip file on site that requires authentication using VBScript


I'm trying to download a ZIP file using VBScript that requires authentication. If you go to the site you'll notice it pops up an authentication prompt. The problem I have is after this script runs the ZIP file is too small for what it should be and is corrupt so I can't open it.

My thought is the download isn't working.

Anyone see what I'm doing wrong?

strHDLocation = "C:\Test\file1.zip"
Set xmlHttp = CreateObject("Microsoft.XMLHTTP")

xmlHttp.Open "GET", "http:downloadsite/report-id=123456", False, "myidhere", "mypwhere"
xmlHttp.Send()


Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary

objADOStream.Write xmlHttp.ResponseBody
objADOStream.Position = 0    'Set the stream position to the start

Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing

objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing

Solution

  • As a bare minimum when using IXmlHttpRequest you should check the Status property to make sure that assumptions are not made about what is being returned.

    If xmlHttp.Status = 200 Then
      'Successful Request
    Else
      'Something went wrong
    End If
    

    It's likely the request has failed for one reason or another and the ResponseBody contains the failed response not the expected ZIP file.