Question

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()   
}
Was it helpful?

Solution

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()    
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top