UrlDownloadToFile 的替代方法 [英] Alternative to UrlDownloadToFile

查看:63
本文介绍了UrlDownloadToFile 的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UrlDownloadToFile 是 AutoHotkey 中的一个不错的命令,并且在大多数情况下都可以正常工作,但有时下载机制对它来说太复杂了.例如,如果下载需要设置特定的用户代理,或者下载需要 cookie 甚至密码.

UrlDownloadToFile is a nice command in AutoHotkey and works just fine, most of the time, but sometimes a download mechanism is too complex for it. For example if the download requires a specific user-agent to be set or if the download requires a cookie or maybe even a password.

所以问题是:
有没有更高级的下载功能,可以处理以上所有问题?

So the question is:
Is there a more advanced download function, which could handle all of the above said?

推荐答案

我很久以前就写过这个,我认为将它封装在一个函数中并张贴在这里是个好主意:

I wrote this quite some time ago and thought it would be a nice idea to wrap it up in a function and post it here:

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")

这篇关于UrlDownloadToFile 的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆