Skip to content

Instantly share code, notes, and snippets.

@sidola
Created May 3, 2014 17:10
Show Gist options
  • Save sidola/4987fbcfc87e4bb006f8 to your computer and use it in GitHub Desktop.
Save sidola/4987fbcfc87e4bb006f8 to your computer and use it in GitHub Desktop.
AHK - Download HTML data to a variable
DownloadToString(url, encoding = "utf-8")
{
static a := "AutoHotkey/" A_AhkVersion
if (!DllCall("LoadLibrary", "str", "wininet") || !(h := DllCall("wininet\InternetOpen", "str", a, "uint", 1, "ptr", 0, "ptr", 0, "uint", 0, "ptr")))
return 0
c := s := 0, o := ""
if (f := DllCall("wininet\InternetOpenUrl", "ptr", h, "str", url, "ptr", 0, "uint", 0, "uint", 0x80003000, "ptr", 0, "ptr"))
{
while (DllCall("wininet\InternetQueryDataAvailable", "ptr", f, "uint*", s, "uint", 0, "ptr", 0) && s > 0)
{
VarSetCapacity(b, s, 0)
DllCall("wininet\InternetReadFile", "ptr", f, "ptr", &b, "uint", s, "uint*", r)
o .= StrGet(&b, r >> (encoding = "utf-16" || encoding = "cp1200"), encoding)
}
DllCall("wininet\InternetCloseHandle", "ptr", f)
}
DllCall("wininet\InternetCloseHandle", "ptr", h)
return o
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment