Help with UrlDownloadToFile

Get help with using AutoHotkey and its commands and hotkeys
dshss
Posts: 9
Joined: 11 Feb 2019, 09:10

Help with UrlDownloadToFile

15 Mar 2019, 05:29

Hi,

I'm starting a script to check a webpage for differences, but I'm hitting a bump right at the beginning.

Code: Select all

;#NoTrayIcon

;=== Variables ==============================================================
	NEW_HTML=%A_WorkingDir%\new.html
	;OLD_HTML=old.html
	URL=http www.google.com   Broken Link for safety
	;SLEEPTIME=30000

;=== Main ===================================================================
	UrlDownloadToFile, %URL%, %NEW_HTML%
	msgbox, %errorlevel%

	ExitApp
Right now, I expected it to just save the html file and exit, but it does nothing, doesn't even show the msgbox and doesn't exit. What am I doing wrong here?

I've searched and I've seen at least one script that does something similar, but I'd like to do this on my own.

Thanks in advance.
dshss
Posts: 9
Joined: 11 Feb 2019, 09:10

Re: Help with UrlDownloadToFile

15 Mar 2019, 07:19

I already checked the variables, and they seem correct.
AviationGuy
Posts: 175
Joined: 17 Jan 2019, 10:13

Re: Help with UrlDownloadToFile

15 Mar 2019, 08:05

Hi,

What if you removed the A_WorkingDir and put the path there?
It worked for me that way.

Code: Select all

HTML=S:\...\new.html
URL=google.com
;=== Main ===================================================================
UrlDownloadToFile, %URL%, %HTML%
msgbox, %errorlevel%                         ; if 1 problem occured, if 0 file has been made
return
MannyKSoSo
Posts: 376
Joined: 28 Apr 2018, 21:59

Re: Help with UrlDownloadToFile

15 Mar 2019, 08:07

How about this

Code: Select all

;Since UrlDownload is an older function it uses regular text as the inputs instead of variables and the output file is located in the same dir
;as the autohotkey location
UrlDownloadToFile, www.google.com, new.html
MsgBox Done!
Exitapp
dshss
Posts: 9
Joined: 11 Feb 2019, 09:10

Re: Help with UrlDownloadToFile

15 Mar 2019, 09:51

Code: Select all

UrlDownloadToFile, http www.google.com , D:\...\new.html
and

Code: Select all

UrlDownloadToFile, www.google.com , D:\...\new.html
didn't work. However, the msgbox printed the errorlevel 1

Is there another option to get the html code from a page?
MannyKSoSo
Posts: 376
Joined: 28 Apr 2018, 21:59

Re: Help with UrlDownloadToFile

15 Mar 2019, 11:16

Code: Select all

;Since UrlDownload is an older function it uses regular text as the inputs instead of variables and the output file is located in the same dir
;as the autohotkey location
location := "new.html"
Website := "https://www.autohotkey.com/boards/viewtopic.php?f=76&t=62776&sid=54f846d2d896a27dbf20df235aa8ff58"
UrlDownloadToFile, %Website%, %location%
MsgBox Done!
Exitapp
dshss
Posts: 9
Joined: 11 Feb 2019, 09:10

Re: Help with UrlDownloadToFile

15 Mar 2019, 12:16

MannyKSoSo wrote:
15 Mar 2019, 11:16

Code: Select all

;Since UrlDownload is an older function it uses regular text as the inputs instead of variables and the output file is located in the same dir
;as the autohotkey location
location := "new.html"
Website := "https://www.autohotkey.com/boards/viewtopic.php?f=76&t=62776&sid=54f846d2d896a27dbf20df235aa8ff58"
UrlDownloadToFile, %Website%, %location%
MsgBox Done!
Exitapp
It takes about 20s and throws "Done", but nothing is downloaded. I'll have to check if it's a issue with the connection. Perhaps the firewall is blocking.
MannyKSoSo
Posts: 376
Joined: 28 Apr 2018, 21:59

Re: Help with UrlDownloadToFile

15 Mar 2019, 13:29

It should make the file in the same location as the .ahk file. I am not sure that the firewall is blocking it
CloakerSmoker
Posts: 12
Joined: 07 Sep 2018, 15:54

Re: Help with UrlDownloadToFile

15 Mar 2019, 15:47

Try using this to download the website's HTML to a variable instead of a file, it should streamline your code a good bit and get around any problems with UrlDownloadToFile.

Code: Select all

URL := "https mywebsite.com /this/isnt/a/real/url/dont/go/to/it"  Broken Link for safety
WinHttp:= ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHttp.Open("GET", URL, false)
WinHttp.Send()
Resp := WinHttp.ResponseText
MsgBox % Resp
dshss
Posts: 9
Joined: 11 Feb 2019, 09:10

Re: Help with UrlDownloadToFile

18 Mar 2019, 03:24

"Error: 0x80072EE2 -
Source: WinHttp.WinHttpRequest
Description: The operation timed out"

I'll try this on a different computer/network...
CloakerSmoker
Posts: 12
Joined: 07 Sep 2018, 15:54

Re: Help with UrlDownloadToFile

18 Mar 2019, 03:31

Err, you did change URL to a valid website, right? The code I sent doesn't work unless you give it a real URL (And remove "Broken Link for safety" from the end of the line)
dshss
Posts: 9
Joined: 11 Feb 2019, 09:10

Re: Help with UrlDownloadToFile

18 Mar 2019, 08:18

Yes :), I changed to https www.google.com and also tried without the https: // Broken Link for safety part
CloakerSmoker
Posts: 12
Joined: 07 Sep 2018, 15:54

Re: Help with UrlDownloadToFile

18 Mar 2019, 10:42

Alright, you never know for sure when people just pull a CTRL-C CTRL-V and run things without changing anything :crazy:.
You might want to check if your antivirus or firewall are blocking AHK.
You can also try adding this line

Code: Select all

WinHttp.SetTimeouts(120000, 120000, 120000, 120000)
above the .Open() line to raise the timeouts to 2 minutes, and also stick this one below .Send()

Code: Select all

WinHttp.WaitForResponse()
Ahk_fan
Posts: 50
Joined: 31 Aug 2018, 14:34

Re: Help with UrlDownloadToFile

18 Mar 2019, 15:23

try this "downloadtoFile" by Bentschi

Code: Select all

URL = https://www.google.com/
DownloadToFile(URL, A_ScriptDir "\new.html")
return

DownloadToFile(url, filename)
{
    static a := "AutoHotkey/" A_AhkVersion
    if (!(o := FileOpen(filename, "w")) || !DllCall("LoadLibrary", "str", "wininet") || !(h := DllCall("wininet\InternetOpen", "str", a, "uint", 1, "ptr", 0, "ptr", 0, "uint", 0, "ptr")))
        return 0
    c := s := 0
    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)
            c += r
            o.rawWrite(b, r)
        }
        DllCall("wininet\InternetCloseHandle", "ptr", f)
    }
    DllCall("wininet\InternetCloseHandle", "ptr", h)
    o.close()
    return c
}

regards,
AHK_fan :)
dshss
Posts: 9
Joined: 11 Feb 2019, 09:10

Re: Help with UrlDownloadToFile

19 Mar 2019, 03:47

That DownloadToFile created an html file, but it was empty.

I'm betting it's something blocking the script.
Thank you all for your help :)
I'm gonna try to work around the connectivity issue or find another way.

Currently, I have a VM where that script works, so that's an option. I have a shared folder and I can get that html code on windows. I just wanted to have it all in one place and not need the VM running, but worst case scenario, it'll stay this way.

Return to “Ask For Help”

Who is online

Users browsing this forum: manehscripts, niczoom, unconnu and 42 guests