UrlDownloadToFile在AutoHotkey中是一个不错的命令,并且在大多数情况下工作得很好,但有时候下载机制太复杂了。例如,如果下载需要设置特定的用户代理,或者下载需要cookie或甚至是密码。
所以问题是:
是否有更高级的下载功能,可以处理上述所有内容?
答案 0 :(得分:3)
我很久以前写过这篇文章,并认为将它包装在一个函数中并将其发布在这里是一个不错的主意:
Download(UrlToFile,SaveFileAs:="",Overwrite:=True,headers:="",method:="GET",postData:="") {
WinHttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHttpObj.Open(method, UrlToFile)
For header, value in headers
WinHttpObj.SetRequestHeader(header, value)
WinHttpObj.Send(postData)
ADODBObj := ComObjCreate("ADODB.Stream")
ADODBObj.Type := 1
ADODBObj.Open()
ADODBObj.Write(WinHttpObj.ResponseBody)
If !SaveFileAs {
urlSplitArray := StrSplit(UrlToFile, "/")
SaveFileAs := urlSplitArray[urlSplitArray.MaxIndex()]
}
ADODBObj.SaveToFile(SaveFileAs, Overwrite ? 2:1)
ADODBObj.Close()
}
示例1
Download("http://ahkscript.org/download/1.1/AutoHotkey111402_Install.exe")
示例2
customHeaders := {"User-Agent": "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0"
,"Cache-Control": "max-age=0"
,"Cookie": "downloadtoken=b82416fdb23e421fb5a"}
Download("http://download.piriform.com/ccsetup410.exe","",True,customHeaders)
示例3
Download("http://foo.bar/example.exe","example.exe",True,{"Cookie":"sessionid=abc123"},"POST","username=foo_bar&password=qwerty")