Find the size of retrieved binary data with WinHttp.WinHttpRequest

半世苍凉 提交于 2019-12-11 03:06:28

问题


I've recently realized that URLDownloadToFile uses the IE proxy setting. So I'm looking for an alternative and found WinHttp.WinHttpRequest may work.

It seems the ResponseBody property contains the fetched data and I need to write it to a file. The problem is that I cannot find the byte size of it.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa384106%28v=vs.85%29.aspx has the information for the object but I don't find relevant properties for it.

Can somebody tell how?

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()   
}

回答1:


I found a way by myself.

Since psfa is a byte array, simply the number of the elements represents its size.

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

However, to save the binary data stored in a safearray, using the file object was not successful. (There might be a way but I could not find it) Instead, ADODB.Stream worked like a charm.

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()    
}


来源:https://stackoverflow.com/questions/13532969/find-the-size-of-retrieved-binary-data-with-winhttp-winhttprequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!