0

当我尝试使用此脚本下载一个空文件时,我收到错误消息:参数类型错误、超出可接受范围或相互冲突。我该如何解决?

这是我的脚本

    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

我只有空文件才有这个问题。

4

1 回答 1

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
于 2014-07-01T15:02:00.213 回答