我最近意识到 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/