替代UrlDownloadToFile [英] Alternative to UrlDownloadToFile

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

问题描述

UrlDownloadToFile是AutoHotkey中的一个不错的命令,大部分时间都可以正常工作,但有时下载机制太复杂了。例如,如果下载需要设置特定的用户代理,或者下载需要cookie或甚至密码。



所以问题是:

是否有更高级的下载功能,可以处理上述所有内容?

解决方案

前一段时间以为这是一个很好的主意,把它放在一个函数中,并在这里发布:

 下载(UrlToFile ,SaveFileAs:=,覆盖:= True,headers:=,方法:=GET,postData:=){
WinHttpObj:= ComObjCreate(WinHttp.WinHttpRequest.5.1 b $ b WinHttpObj.Open(方法,UrlToFile)
标题中的值
WinHttpObj.SetRequestHeader(头,值)
WinHttpObj.Send(postData)

ADODBObj:= ComObjCreate(ADODB.Stream)
ADODBObj.Type:= 1
ADODBObj.Open()
ADODBObj.Write(WinHttpObj.ResponseBody)
如果!SaveFileAs {
urlSplitArra y:= StrSplit(UrlToFile,/)
SaveFileAs:= urlSplitArray [urlSplitArray.MaxIndex()]
}
ADODBObj.SaveToFile(SaveFileAs,Overwrite? 2:1)
ADODBObj.Close()
}

示例1

 下载(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
,缓存控制:max-age = 0
,Cookie:downloadtoken = b82416fdb23e421fb5a}
下载(http://download.piriform.com/ccsetup410.exe ,True,customHeaders)

示例3

 下载(http://foo.bar/example.exe,example.exe,True,{Cookie:sessionid = abc123},POST,username = foo_bar& password = qwerty)


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

Example 1

Download("http://ahkscript.org/download/1.1/AutoHotkey111402_Install.exe")

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

Example 3

Download("http://foo.bar/example.exe","example.exe",True,{"Cookie":"sessionid=abc123"},"POST","username=foo_bar&password=qwerty")

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

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