Faster, error processing, timeouts, referrer, proxy, cookie, UA, ...
Working example:
Code: Select all
Request:={}, i:=0
Request[++i] := ["http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"]
Request[++i] := ["http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js",,,, timeout:=0.3] ; too small
; Request[++i] := ["http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js",,,, timeout:=1.5]
Request[++i] := ["http://ajax.google-shmoogle.com"]
Request[++i] := ["http://ajax.googleapis.com/ho-ho!"]
Request[++i] := ["http://www.tingchina.com/play/yousheng/flash.asp?id=24366&inum=1&flei=恐怖惊悚&bookname=我当阴曹官的那几年&filename=001.mp3&rand=16&nexturl=play_24366_1.htm"] ; not so easy to get this page
Request[++i] := ["http://www.tingchina.com/play/yousheng/flash.asp?id=24366&inum=1&flei=恐怖惊悚&bookname=我当阴曹官的那几年&filename=001.mp3&rand=16&nexturl=play_24366_1.htm", Referer:="http://www.tingchina.com/yousheng/24366/play_24366_0.htm"]
Request[++i] := ["http://www.tingchina.com/play/yousheng/flash.asp?id=24366&inum=1&flei=恐怖惊悚&bookname=我当阴曹官的那几年&filename=001.mp3&rand=16&nexturl=play_24366_1.htm", Referer:="http://www.tingchina.com/yousheng/24366/play_24366_0.htm",,,,,,,, Charset:="gb2312"]
Loop % i ;Request.MaxIndex()
MsgBox,, % ((answer:=UrlDownloadToVar(Request[A_Index]*))=false ? "Error:" : "Success:") " " (URL:=Request[A_Index][1]), % answer=false ? ErrorLevel : answer
ExitApp
Esc::ExitApp
Return
UrlDownloadToVar(URL, Referer="", UserAgent="", Cookie="", TimeoutSec=-1, Proxy="", ProxyBypassList="", EnableRedirects="", URLCodePage="", Charset="") {
; If (Error) {put explanation in ErrorLevel; return false;}
; autors: stealzy, tuzi
; https://autohotkey.com/boards/viewtopic.php?f=6&t=12368&p=64186
; More about WinHttpRequest: https://msdn.microsoft.com/en-us/library/windows/desktop/aa383979%28v=vs.85%29.aspx
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
t := URLCodePage ? WebRequest.Option(2):=URLCodePage :
t := (EnableRedirects <> "") ? WebRequest.Option(6):=EnableRedirects :
t := Proxy ? WebRequest.SetProxy(2,Proxy,ProxyBypassList) :
; WebRequest.SetTimeouts(ResolveTimeout:=0, ConnectTimeout:=60000, SendTimeout:=30000, ReceiveTimeout:=30000) ; time in ms
; https://msdn.microsoft.com/library/windows/desktop/aa384061%28v=vs.85%29.aspx
try WebRequest.Open("GET", URL, true)
catch Error {
ErrorLevel := "Wrong URL format"
return false
}
t := Cookie ? WebRequest.SetRequestHeader("Cookie", Cookie) :
t := Referer ? WebRequest.SetRequestHeader("Referer", Referer) :
t := UserAgent ? WebRequest.SetRequestHeader("User-Agent", UserAgent) :
WebRequest.Send()
t := A_TickCount
try Suc:=WebRequest.WaitForResponse(TimeoutSec+0) ; Timeout in seconds; without "+0" interpreted as string
; if no internet access, Timeout ≈ 21-23 sec
catch Error {
OutputDebug % "Error WaitForResponse: " A_TickCount - t
ErrorLevel := "No internet access / No existing domain"
return false
}
OutputDebug % "Success WaitForResponse: " A_TickCount - t
try HTTPStatusCode := WebRequest.Status
catch Error {
ErrorLevel := "WebRequest.Status not ready = timeout expired"
return false
}
if (SubStr(HTTPStatusCode, 1, 1) ~= "4|5") { ; 4xx — Client Error, 5xx — Server Error. wikipedia.org/wiki/List_of_HTTP_status_codes
ErrorLevel := "Error HTTP Status Code: " HTTPStatusCode
return false
}
If (Charset="") {
try ResponseText := WebRequest.ResponseText()
catch Error {
ErrorLevel := " WebRequest.ResponseText not ready = timeout expired"
return false
}
} Else {
ADO := ComObjCreate("adodb.stream")
ADO.Type := 1
ADO.Mode := 3
ADO.Open()
ADO.Write(WebRequest.ResponseBody())
ADO.Position := 0
ADO.Type := 2
ADO.Charset := Charset
ResponseText := ADO.ReadText()
}
return ResponseText
}