Vbscript 通过vbs下载文件

Vbscript 通过vbs下载文件,vbscript,Vbscript,我知道这已经被问到和回答,这是我找到的代码开始我的项目。但它不起作用 我卡住了。我试了又试,但代码不起作用 Dim xHttp: Set xHttp = CreateObject("Microsoft.XMLHTTP") Dim bStrm: Set bStrm = CreateObject("Adodb.Stream") xHttp.Open "GET", "https://filexxx.exe", False xHttp.Send With bStrm .Type = 1 'bi

我知道这已经被问到和回答,这是我找到的代码开始我的项目。但它不起作用

我卡住了。我试了又试,但代码不起作用

Dim xHttp: Set xHttp = CreateObject("Microsoft.XMLHTTP")
Dim bStrm: Set bStrm = CreateObject("Adodb.Stream")
xHttp.Open "GET", "https://filexxx.exe", False
xHttp.Send

With bStrm
    .Type = 1 'binary
    .Open
    .Write xHttp.ResponseBody 'this part removed, error, tools not yet avail
    .SaveToFile "c:\temp\xxx.exe", 2 'overwrite
End With
还有这个

strFileURL = "https://filexxx.exe"
strHDLocation = "C:\temp\xxx.exe"

Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")

objXMLHTTP.Open "GET", strFileURL, False
objXMLHTTP.Send()

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

objADOStream.SaveToFile strHDLocation
strFileURL = "https://filexxx.exe"
strHDLocation = "C:\temp\filexxx.exe"
proxy = null
Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Set wshShell = CreateObject( "WScript.Shell" )
Set objUserVariables = wshShell.Environment("USER")

'http proxy is optional
'attempt to read from HTTP_PROXY env var first
On Error Resume Next

If Not (objUserVariables("HTTP_PROXY") = "") Then
    proxy = objUserVariables("HTTP_PROXY")
ElseIf Not (WScript.Arguments.Named("proxy") = "") Then
    proxy = WScript.Arguments.Named("proxy")
End If

If Not isNull(proxy) Then
    Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    objXMLHTTP.SetProxy 2, proxy
End If

On Error Goto 0

objXMLHTTP.Open "GET", strFileURL, False
objXMLHTTP.Send()
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1
objADOStream.Position = 0
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(path) Then objFSO.DeleteFile path
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
Set objXMLHTTP = Nothing
还有这个

strFileURL = "https://filexxx.exe"
strHDLocation = "C:\temp\xxx.exe"

Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")

objXMLHTTP.Open "GET", strFileURL, False
objXMLHTTP.Send()

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

objADOStream.SaveToFile strHDLocation
strFileURL = "https://filexxx.exe"
strHDLocation = "C:\temp\filexxx.exe"
proxy = null
Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Set wshShell = CreateObject( "WScript.Shell" )
Set objUserVariables = wshShell.Environment("USER")

'http proxy is optional
'attempt to read from HTTP_PROXY env var first
On Error Resume Next

If Not (objUserVariables("HTTP_PROXY") = "") Then
    proxy = objUserVariables("HTTP_PROXY")
ElseIf Not (WScript.Arguments.Named("proxy") = "") Then
    proxy = WScript.Arguments.Named("proxy")
End If

If Not isNull(proxy) Then
    Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    objXMLHTTP.SetProxy 2, proxy
End If

On Error Goto 0

objXMLHTTP.Open "GET", strFileURL, False
objXMLHTTP.Send()
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1
objADOStream.Position = 0
Set objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(path) Then objFSO.DeleteFile path
Set objFSO = Nothing
objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
Set objXMLHTTP = Nothing
这个呢

HTTPDownload "https:filexxx.exe", "C:\temp\filexxx.exe"

Sub HTTPDownload(myURL, myPath)
    ' Standard housekeeping
    Dim i, objFile, objFSO, objHTTP, strFile, strMsg
    Const ForReading = 1, ForWriting = 2, ForAppending = 8

    ' Create a File System Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Check if the specified target file or folder exists,
    ' and build the fully qualified path of the target file
    If objFSO.FolderExists(myPath) Then
        strFile = objFSO.BuildPath(myPath, Mid(myURL, InStrRev(myURL, "/") + 1))
    ElseIf objFSO.FolderExists(Left(myPath, InStrRev(myPath, "\") - 1)) Then
        strFile = myPath
    Else
        WScript.Echo "ERROR: Target folder not found."
        Exit Sub
    End If

    ' Create or open the target file
    Set objFile = objFSO.OpenTextFile(strFile, ForWriting, True)

    ' Create an HTTP object
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")

    ' Download the specified URL
    objHTTP.Open "GET", myURL, False
    objHTTP.Send

    ' Write the downloaded byte stream to the target file
    For i = 1 To LenB(objHTTP.ResponseBody)
        objFile.Write Chr(AscB(MidB(objHTTP.ResponseBody, i, 1)))
    Next

    ' Close the target file
    objFile.Close()
End Sub
都有相同的结果。他们不下载文件。有时其中一个会冻结电脑,我不得不手动关机

他们会在一段时间后下载(但不会),并且只显示为0kb的文件。如果我改变

objXMLHTTP.Open "GET", strFileURL, False

它立即显示在文件夹中,并显示0kb

不管是真是假,等待30分钟以上对文件的大小并没有任何影响。实际文件大小为1874886KB,从网站下载只需几分钟

Dim ie
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.Navigate2("https://filexxx.exe")
ie.Document.Execwb("saveas", False, "C:\temp\filexxxx.exe")
ie.Quit
给予

调用sub时,第5行字符58不能使用括号

如果我从驱动器\文件夹\文件中删除引号“”,我将得到

第5行字符36“


注意URL是https,而不是http。我已经为此工作了几天。

我不知道发布随机代码有什么意义。这段代码的要点是它说明了正在发生的事情

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Outp = Wscript.Stdout
    Set wshShell = CreateObject("Wscript.Shell")
    Set ShApp = CreateObject("Shell.Application")
    On Error Resume Next
    Set File = WScript.CreateObject("Msxml2.XMLHTTP.6.0")
    File.Open "GET", "https://www.google.com.au/search?q=cat"), False
    File.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C; .NET4.0E; BCD2000; BCD2000)"
    File.Send

        wscript.echo "" 
        wscript.echo "Error getting file" 
        wscript.echo "==================" 
        wscript.echo "" 
        wscript.echo "Error " & err.number & "(0x" & hex(err.number) & ") " & err.description 
        wscript.echo "Source " & err.source 
        wscript.echo "" 
        wscript.echo "HTTP Error " & File.Status & " " & File.StatusText
        wscript.echo    File.getAllResponseHeaders
        wscript.echo    File.ResponseBody

        On Error Goto 0
        wscript.echo "Server Response " & File.Status & " " & File.StatusText
        wscript.echo    File.getAllResponseHeaders
        Set BS = CreateObject("ADODB.Stream")
        BS.type =1
'       BS.Charset = "utf-8"
        BS.open
        BS.Write File.ResponseBody
        BS.Position = 0
        BS.type =2
        BS.Charset = "utf-8"
        wscript.echo    BS.ReadText

        Set BS = CreateObject("ADODB.Stream")
        BS.type = 1
        BS.open
        BS.Write File.ResponseBody
        BS.SaveToFile ShApp.Namespace(&h10).self.path & "\Google.html]", 2
'       wshshell.Run "c:\users\safetyscanner.exe", 1, False

https://file.exe
不是有效的URL,除非存在承载域
文件.exe的Web服务器,您要从该域下载索引文档。您的前两个代码示例基本上也是这样做的#3添加了代理处理,只有在实际通过代理连接时才需要#2和#3无法工作,b/c他们在保存文件之前不会将响应写入流#4逐字节写入响应#5等于1。我希望#1和#4可以工作,除非您的URL无效。设置第三个。。。[1/3][2/3] ... 将方法的参数设置为True不会有任何帮助,b/c将异步运行请求,也就是说,代码的其余部分不会等待请求返回响应(这就是为什么最终文件大小为零)。请原谅我在这里直言不讳,但在不了解代码实际功能的情况下,向问题抛出随机代码片段不会让您走得很远。不管怎样,就像我说的:#1和#2应该在AFAICS工作。如果他们没有你需要。。。[2/3][3/3] ... 提供更多关于从它们中得到的任何错误的信息,以及更好的URL字符串示例。如果必须的话,请混淆域名(最好是域名)和文件名,但保持URL的结构不变。我的意思是“1和4应该可以工作”。我又试了一次,然后在4运行时上床睡觉。第二天早上醒来发现没有无线网络。无法打开它,担心我必须再次安装windows。经过今天的几次尝试,两次完全关闭后,wifi再次工作。文件仍未下载。错误行7应为语句结尾。已修复语句结尾。当通过链接进行复制时,我将。但我不知道它将文件保存到哪里,它冻结了我的电脑,就像其他尝试发布的一样#1和#4它是否打印任何消息。是的,第7行是语句的结尾。当然,我把谷歌搜索猫的链接改成了我选择的下载链接。