当我尝试使用此脚本下载空文件时,我收到错误消息: 参数类型错误,超出可接受的范围或彼此冲突。我该如何解决?
这是我的剧本
Set objHTTP = CreateObject("WinHTTP.WinHttpRequest.5.1")
objHTTP.Open "GET", "http://localhost/file.txt", False
objHTTP.Send
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Type = 1
.Open
.Write objHTTP.ResponseBody
.SaveToFile "C:\file.txt"
.Close
End With
Set objStream = Nothing
我只有空文件才有这个问题。
答案 0 :(得分:2)
对于空文件,.ResponseBody是子类型Empty的变体。这样的野兽不能写。由于无法在VBScript中创建空的Byte(),因此必须跳过.Write以获取空文件。在代码中:
Const adSaveCreateNotExist = 1 ' Default. Creates a new file if the file does not already exist
Const adSaveCreateOverWrite = 2 ' Overwrites the file with the data from the currently open Stream
' object, if the file already exists
Set objHTTP = CreateObject("WinHTTP.WinHttpRequest.5.1")
objHTTP.Open "GET", "http://gent/empty.html", False
objHttp.Send
WScript.Echo objHttp.Status, objHttp.StatusText
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Type = 1
.Open
WScript.Echo TypeName(objHTTP.ResponseBody)
If Not IsEmpty(objHTTP.ResponseBody) Then .Write objHTTP.ResponseBody
.SaveToFile "file.txt", adSaveCreateOverWrite
.Close
End With
Set objStream = Nothing
输出:
cscript 24512602.vbs
200 OK
Empty
...
dir
...
01.07.2014 16:54 771 24512602.vbs
01.07.2014 16:54 0 file.txt