Skip to content

Instantly share code, notes, and snippets.

@tmplinshi
Forked from grey-code/gist:7042282
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmplinshi/9222424 to your computer and use it in GitHub Desktop.
Save tmplinshi/9222424 to your computer and use it in GitHub Desktop.
Download(url, dest)
{
static oHttp := ComObjCreate("WinHttp.WinHttpRequest.5.1")
oHttp.Open("GET", url, True)
oHttp.Send()
If !oHttp.WaitForResponse(-1) ; Success = -1, Timeout = 0, No response = Empty String
Return False
if ( ComObjType(oHttp.ResponseStream) = 0xD ) ; VT_UNKNOWN = 0xD
{
p := ComObjQuery(oHttp.ResponseStream, "{0000000c-0000-0000-C000-000000000046}")
FileSize := oHttp.GetResponseHeader("Content-Length")
VarSetCapacity(bin, FileSize)
r := DllCall(NumGet(NumGet(p+0)+3*A_PtrSize), "Ptr", p, "Ptr", &bin, "UInt", FileSize, "Ptr*", c)
FileOpen(dest, "w").RawWrite(&bin, c)
ObjRelease(p)
}
else
FileOpen(dest, "w").Write(oHttp.ResponseText)
Return True
}
/*
; Derived from Wicked - can't locate his thread but found this in the ff thread:
; http://www.autohotkey.com/board/topic/97642-urldownloadtofile/#entry614852
Download(url, dest) {
static r
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
if (!r || whr.Option(1) != url)
whr.Open("GET", url)
whr.Send()
if (whr.ResponseText = "failed" || whr.Status != 200 || ComObjType(whr.ResponseStream) != 0xd)
return false
p := ComObjQuery(whr.ResponseStream, "{0000000c-0000-0000-C000-000000000046}")
file := FileOpen(dest, "w")
Loop {
VarSetCapacity(bin, 8192)
r := DllCall(NumGet(NumGet(p+0)+3*A_PtrSize), "Ptr", p, "Ptr", &bin, "UInt", 8192, "Ptr*", c)
file.RawWrite(&bin, c)
} until (c == 0)
ObjRelease(p)
file.Close()
return whr.ResponseText
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment