2012-11-23 48 views
2

我最近意識到URLDownloadToFile使用IE代理設置。所以我正在尋找替代方法,並發現WinHttp.WinHttpRequest可能工作。用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() 
} 

回答

2

我自己找到了一種方法。

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