com - 使用 WinHttp.WinHttpRequest 查找检索到的二进制数据的大小

标签 com autohotkey winhttp winhttprequest

我最近意识到 URLDownloadToFile 使用 IE 代理设置。所以我正在寻找替代方案,发现 WinHttp.WinHttpRequest 可能有效。

看来 ResponseBody 属性包含获取的数据,我需要将其写入文件。问题是我找不到它的字节大小。

http://msdn.microsoft.com/en-us/library/windows/desktop/aa384106%28v=vs.85%29.aspx有该对象的信息,但我没有找到它的相关属性。

谁能告诉我怎么做吗?

strURL := "http://www.mozilla.org/media/img/sandstone/buttons/firefox-large.png"
strFilePath := A_ScriptDir "\dl.jpg"

pwhr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
pwhr.Open("GET", strURL) 
pwhr.Send() 

if (psfa := pwhr.ResponseBody ) {   
    oFile := FileOpen(strFilePath, "w")
    ; msgbox % ComObjType(psfa) ; 8209 
    oFile.RawWrite(psfa, strLen(psfa)) ; not working
    oFile.Close()   
}

最佳答案

我自己找到了方法。

由于 psfa 是一个字节数组,因此元素的数量就代表了它的大小。

msgbox % psfa.maxindex() + 1    ; 17223 bytes for the example file. A COM array is zero-based so it needs to add one.

但是,要保存存储在 safearray 中的二进制数据,使用文件对象并不成功。 (可能有办法,但我找不到)相反,ADODB.Stream 工作得非常顺利。

strURL := "http://www.mozilla.org/media/img/sandstone/buttons/firefox-large.png"
strFilePath := A_ScriptDir "\dl.png"
bOverWrite := true

pwhr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
pwhr.Open("GET", strURL) 
pwhr.Send() 

if (psfa := pwhr.ResponseBody ) {   
    pstm := ComObjCreate("ADODB.Stream")
    pstm.Type() := 1        ; 1: binary 2: text
    pstm.Open()
    pstm.Write(psfa)
    pstm.SaveToFile(strFilePath, bOverWrite ? 2 : 1)
    pstm.Close()    
}

关于com - 使用 WinHttp.WinHttpRequest 查找检索到的二进制数据的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13532969/

相关文章:

autohotkey - 两行代码的冲突使代码无法按照我的意思工作,我可以做些什么不同的事情来完成这项工作(?)

c++ - 如何在 C/C++ 中使用 WinHTTP 下载文件?

c# - 在 C# 中将字符串转换为字节数组以在 C++ 中将字符串写入文件

c# - .NET2.0 C# 互操作 : How to call COM code from C#?

excel - 通过 Matlab 在 Excel 中插入图像

http - 如何防止 ServerXMLHTTP 自动跟随重定向(HTTP 303 请参阅其他响应)?

c++ - 上传文件时的 WinHTTP 问题

c# - Sharepoint 字段或属性 "Body"不存在 ServerException

autohotkey - 带 ALT 的特殊模式,如大写锁定

vim - 如果 getKeyState 不起作用,自动热键将 alt+j 重新映射到左侧,但 alt+shift+j 不会选择左侧,也不会 alt+ctr+j 移动插入符号