在需要使用 VBScript 进行身份验证的站点上下载 zip 文件

Download zip file on site that requires authentication using VBScript

我正在尝试使用需要身份验证的 VBScript 下载 ZIP 文件。如果您访问该站点,您会注意到它会弹出身份验证提示。我遇到的问题是此脚本运行后 ZIP 文件太小,无法打开它,而且已损坏,因此我无法打开它。

我的想法是下载失败。

有人看到我做错了什么吗?

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

作为最低限度使用 IXmlHttpRequest 时,您应该检查 Status 属性 以确保没有对返回的内容做出假设。

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

请求可能由于某种原因而失败,ResponseBody 包含失败的响应而不是预期的 ZIP 文件。