IWinHttpRequest::Open Method Successful?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Wicked
Posts: 40
Joined: 05 Jun 2016, 20:34

IWinHttpRequest::Open Method Successful?

17 Nov 2017, 15:30

Hi everybody. Long time.

I'll keep it short:

Code: Select all

_download_to_var(url){
	req:=comobjcreate("WinHttp.WinHttpRequest.5.1")
	;~ https://msdn.microsoft.com/en-us/library/windows/desktop/aa383989(v=vs.85).aspx
	req.open("GET",url) ;~ <= Returns nothing, every time, regardless if url is resolved...
	req.send() ;~ <= Error if "url" could not be resolved...
	;~ ...


What's the best way to add a fail-safe so that if the URL doesn't exist it returns false or exits before send()? I could use comobjerror(), I suppose? I'm primarily looking for speed, as I'll be doing multiple back-to-back requests.

Thanks in advance.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: IWinHttpRequest::Open Method Successful?

22 Nov 2017, 00:09

Hi Wicked,

For example:

Code: Select all


; based on Class HTTP.ahk > https://github.com/Run1e/Vibrancer/blob/master/lib/Class%20HTTP.ahk
_download_to_var("egvjhev")
MsgBox % ErrorLevel
_download_to_var("https://autohotkey.com/download/1.1/version.txt")
MsgBox % ErrorLevel

_download_to_var(__url, __timeout:=5) {
static __winHttpRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1"), INTERNET_CONNECTION_PROXY := "0x40"
		
	if not (DllCall("Wininet.dll\InternetGetConnectedState", "Str", INTERNET_CONNECTION_PROXY, "Int", 0)) ; https://autohotkey.com/board/topic/80587-how-to-find-internet-connection-status/
	return !ErrorLevel:=-2

	try __winHttpRequest.Open("GET", __url, true)
	catch {
	return !ErrorLevel:=-1
	}
	__winHttpRequest.Send()
		
	try {
		if (__winHttpRequest.WaitForResponse(__timeout)) { ; https://msdn.microsoft.com/en-us/library/windows/desktop/aa384064(v=vs.85).aspx
			return !ErrorLevel:=0
		}
	}
	return !ErrorLevel:=1
	
}
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5, mikeyww, RandomBoy and 384 guests