WinHttpRequest issue Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Youri11
Posts: 23
Joined: 23 Apr 2021, 08:57

WinHttpRequest issue

Post by Youri11 » 06 Dec 2023, 09:19

Hello,

When I run the following code picked from AHK documentation examples :

Code: Select all

whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "https://www.autohotkey.com/download/1.1/version.txt", true)
whr.Send()
; Using 'true' above and the call below allows the script to remain responsive.
whr.WaitForResponse()
version := whr.ResponseText
MsgBox % version
I get the following error :
Error: 0x80072F7D -
Source: WinHttp.WinHttpRequest
Description: Une erreur s’est produite lors de la connexion sécurisée


HelpFile: (null)
HelpContext: 0

Specifically: WaitForResponse

Line#
001: whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
002: whr.Open("GET", "https://www.autohotkey.com/download/1.1/version.txt", true)
003: whr.Send()
---> 005: whr.WaitForResponse()
006: version := whr.ResponseText
007: MsgBox,version
008: Exit
009: Exit
009: Exit
What is the problem ? Do you have the same error when you run this code ?
Thank you for any help.

Youri11
Posts: 23
Joined: 23 Apr 2021, 08:57

Re: WinHttpRequest issue

Post by Youri11 » 09 Dec 2023, 11:49

No idea ? :|

gregster
Posts: 9121
Joined: 30 Sep 2013, 06:48

Re: WinHttpRequest issue

Post by gregster » 09 Dec 2023, 12:31

Works here.
Perhaps a firewall or antivirus is intervening...

garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: WinHttpRequest issue

Post by garry » 09 Dec 2023, 16:17

also an example

Code: Select all

;-Use ResponseBody instead of ResponseText ( user teadrinker )
;-------- saved at sunday, 2. June 2019 20:41:42 --------------
;-------- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=65055 ---
F1:="https://autohotkey.com/download/1.1/version.txt"
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.SetTimeouts(500,500,500,500)
whr.Open("GET",F1, true)
whr.Send()
whr.WaitForResponse()
arr := whr.responseBody
pData := NumGet(ComObjValue(arr) + 8 + A_PtrSize)
length := arr.MaxIndex() + 1
text := StrGet(pData, length, "utf-8")
MsgBox,LAST AHK-VERSION-1=`n%text%
return

Youri11
Posts: 23
Joined: 23 Apr 2021, 08:57

Re: WinHttpRequest issue

Post by Youri11 » 09 Dec 2023, 16:59

gregster wrote:
09 Dec 2023, 12:31
Works here.
Perhaps a firewall or antivirus is intervening...
Thank you for your test and your suggestions. Now I know it is likely an issue with my system and not an outdated code.
I've just tried with my firewall disabled and it does not make any difference unfortunately.

garry wrote: also an example

Code: Select all

;-Use ResponseBody instead of ResponseText ( user teadrinker )
;-------- saved at sunday, 2. June 2019 20:41:42 --------------
;-------- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=65055 ---
F1:="https://autohotkey.com/download/1.1/version.txt"
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.SetTimeouts(500,500,500,500)
whr.Open("GET",F1, true)
whr.Send()
whr.WaitForResponse()
arr := whr.responseBody
pData := NumGet(ComObjValue(arr) + 8 + A_PtrSize)
length := arr.MaxIndex() + 1
text := StrGet(pData, length, "utf-8")
MsgBox,LAST AHK-VERSION-1=`n%text%
return
Thank you for your suggestion.
Same issue and same error with this alternate response method unfortunately.

Code: Select all

Error:  0x80072F7D - 
Source:		WinHttp.WinHttpRequest
Description:	Une erreur s’est produite lors de la connexion sécurisée


HelpFile:		(null)
HelpContext:	0

Specifically: WaitForResponse

	Line#
	004: F1 := "https://autohotkey.com/download/1.1/version.txt"
	005: whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	006: whr.SetTimeouts(500,500,500,500)  
	007: whr.Open("GET",F1, true)  
	008: whr.Send()  
--->	009: whr.WaitForResponse()  
	010: arr := whr.responseBody
	011: pData := NumGet(ComObjValue(arr) + 8 + A_PtrSize)
	012: length := arr.MaxIndex() + 1
	013: text := StrGet(pData, length, "utf-8")
	014: MsgBox,LAST AHK-VERSION-1=
%text%
	015: Return
	016: Exit

garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: WinHttpRequest issue

Post by garry » 09 Dec 2023, 17:16

You get message > An error occurred during secure connection
can also try with another url , but this not seems the problem ...
I use only the built in antivirus program from microsoft win-11

Code: Select all

url :="https://www.timeanddate.com/worldclock/"
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
;-
   Whr.Open("GET", url, true)
   Whr.Send()
   Whr.WaitForResponse()
   Arr   := Whr.responseBody
   pData := NumGet(ComObjValue(arr) + 8 + A_PtrSize)
   length:= arr.MaxIndex() + 1
   aab   := StrGet(pData, length, "UTF-8")
;---------------------------------------------------------
;- <a href="/worldclock/honduras/tegucigalpa">Tegucigalpa</a><span id=p131s class=wds></span></td><td id=p131 class=rbi>Fr 12:46</td></tr><tr><td><a href=
;-  cut text
A1= <a href="/worldclock/honduras/tegucigalpa">   ;- from
A2:="</td></tr><tr><td><a href="                  ;- until
;-           
text:=xStr(aab,,a1,a2)
aac:= RegExReplace(text, "[<].*?>")    ;-- GET > TegucigalpaFr 12:46
msgbox,%aac%
exitapp
;-
;======================== user SKAN xStr =========================
;- xStr  for general text extraction and parsing XML  HTML 
;- https://www.autohotkey.com/boards/viewtopic.php?f=6&t=74050
xStr(ByRef H, C:=0, B:="", E:="",ByRef BO:=1, EO:="", BI:=1, EI:=1, BT:="", ET:="") {                           
Local L, LB, LE, P1, P2, Q, N:="", F:=0                 ; xStr v0.97 by SKAN on D1AL/D343 @ tiny.cc/xstr  
Return SubStr(H,!(ErrorLevel:=!((P1:=(L:=StrLen(H))?(LB:=StrLen(B))?(F:=InStr(H,B,C&1,BO,BI))?F+(BT=N?LB
:BT):0:(Q:=(BO=1&&BT>0?BT+1:BO>0?BO:L+BO))>1?Q:1:0)&&(P2:=P1?(LE:=StrLen(E))?(F:=InStr(H,E,C>>1,EO=N?(F
?F+LB:P1):EO,EI))?F+LE-(ET=N?LE:ET):0:EO=N?(ET>0?L-ET+1:L+1):P1+EO:0)>=P1))?P1:L+1,(BO:=Min(P2,L+1))-P1)  
}
;------------------------------------------------------------------

Youri11
Posts: 23
Joined: 23 Apr 2021, 08:57

Re: WinHttpRequest issue

Post by Youri11 » 09 Dec 2023, 17:41

Thank you!! This one works!

Code: Select all

Tegucigalpazat 16:43
After trying, all previous examples also work with the url https://www.timeanddate.com/worldclock/.

This one and all others fail with https://autohotkey.com/download/1.1/version.txt.

Now I get the issue is with https://autohotkey.com/download/1.1/version.txt

garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: WinHttpRequest issue  Topic is solved

Post by garry » 10 Dec 2023, 15:37

example with ActiveX

Code: Select all

Gui,4:Color, Black,Black
Gui,4:Font,cYellow s13,Lucida Console
xxa=Shell.Explorer                   ;- IExplorer
F1    :="https://autohotkey.com/download/1.1/version.txt"
Gui,4: Add, ActiveX, x1 y40 w800 h60 vWbxx,%xxa%
;--- versionx ansi or unicode ---------
if (A_IsUnicode)
  codex := " Encoding is Unicode"
else
  codex := " Encoding is ANSI"
aa :=  ( InStr( (v:=A_AhkVersion), "1.1" ) ? "ahk_L " : "ahk_Basic " ) v  codex
Gui,4: add,text,x10 y5    ,Actual AHK-VERSION  =
Gui,4: add,text,x10 y120  ,Your AHK-version is = %aa%
WBxx.Navigate(F1)
Gui,4: Show,center h220
return
;---------------------------------
4GuiClose:
exitapp
;=================================
URL download to FILE

Code: Select all

;-------- saved at zondag, 6. Februari 2018 21:48:34 user tmplinshi ----
;-------- https://autohotkey.com/boards/viewtopic.php?f=5&t=43852 -------
setworkingdir,%a_scriptdir%
A =%A_AHKVERSION%

f1:="https://autohotkey.com/download/1.1/version.txt"
SplitPath,f1, name, dir, ext, name_no_ext, drive
fd:=a_desktop                                         ;- save here

whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.SetTimeouts(3000,3000,3000,3000)
global whr,fd

url:=WinHttp_UrlDownloadToFile(F1)
xxc=%fd%\%name%

fileread,B,%xxc%
   if (A<>B)
     {
     msgbox, 262436,AHK-Version ,(Changed)`nYour existing version is=%A%`n          Actual version is=%B%`n64-bit=%a_is64bitos%`nUnicode=%a_isunicode%`nOS=%a_osversion%`nWant you open Autohotkey downloads page ?
     IfMsgBox,No
       return
     Else
       {
       run,https://autohotkey.com/download/
       ;run,https://autohotkey.com/download/ahk-install.exe
       return
       }
     }
   else
     {
     msgbox, 262436,AHK-Version ,(Equal)`nYour existing version is=%A%`n           Actual version is=%B%`n64-bit=%a_is64bitos%`nUnicode=%a_isunicode%`nOS=%a_osversion%`nWant you open Autohotkey downloads page ?
     IfMsgBox,No
       return
     Else
       run,https://autohotkey.com/download/
    return
    }
return

;------------ urldownloadtofile user tmplinshi --------------------------------
WinHttp_UrlDownloadToFile(URL, Filename := "") {
	;WHR := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	WHR.Open("GET", url, true)
	WHR.Send()
	WHR.WaitForResponse()

	if (Filename ~= "^\s*$") {
		try {
			RegExMatch( WHR.GetResponseHeader("Content-Disposition"), "filename=\K.+", Filename )
		} catch {
			RegExMatch( WHR.Option(1), "[^/]+$", Filename )    ;- 1 = WinHttpRequestOption_URL
		}
	}
	ADO := ComObjCreate("ADODB.Stream")
	ADO.Type := 1 ; adTypeBinary
	ADO.Open()
	ADO.Write(WHR.ResponseBody)
	ADO.SaveToFile(fd . "\" . FileName, 2)
	ADO.Close()
}
;========================================================================

Youri11
Posts: 23
Joined: 23 Apr 2021, 08:57

Re: WinHttpRequest issue

Post by Youri11 » 11 Dec 2023, 11:09

Thank you Garry.

First example is interesting as unless others, it runs without error message.
The GUI informs me that "Navigation to the webpage was canceled".

Second has the same issue as every others :

Code: Select all

Error:  0x80072F7D - 
Source:		WinHttp.WinHttpRequest
Description:	Une erreur s’est produite lors de la connexion sécurisée


HelpFile:		(null)
HelpContext:	0

Specifically: WaitForResponse

	Line#
	036: Run,https://autohotkey.com/download/
	037: Return
	038: }
	039: Return
	042: {
	044: WHR.Open("GET", url, true)  
	045: WHR.Send()  
--->	046: WHR.WaitForResponse()  
	048: if (Filename ~= "^\s*$")  
	048: {
	049: Try
	049: {
	050: RegExMatch( WHR.GetResponseHeader("Content-Disposition"), "filename=\K.+", Filename )  
	051: }
	051: Catch
I also know now that I face the same error for any pages on autohotkey.com domain, and could face the same for some other domains I tried.

My conclusion is that the WinHttpRequest works and the issue come from my system. I will not rely on this autohotkey feature for now.

Thank you all for your help and examples, this was instructive.

All the best to you.

garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: WinHttpRequest issue

Post by garry » 11 Dec 2023, 17:04

something different ...

Code: Select all

url:="https://autohotkey.com/download/1.1/version.txt"
F1 := a_scriptdir . "\ahk1version.txt"
urldownloadtofile,%url%,%f1%
fileread,aa,%f1%
msgbox,%aa%
exitapp

from user @tmplinshi

Code: Select all

;- from user tmplinshi 
;-
F1:="https://autohotkey.com/download/1.1/version.txt"
;-
;r := WinHttpRequest(F1, InOutData := "", InOutHeaders := Headers(), "Timeout: 1`nNO_AUTO_REDIRECT")
r := WinHttpRequest(F1, InOutData := "", InOutHeaders := Headers(), "Timeout: 10`nAUTO_REDIRECT")

a1:=(r = -1) ? "successful" : (r = 0) ? "Timeout" : "No response"
a2:=InOutData
a3:=InOutHeaders
msgbox,%a1%`n---------------------`n%a2%`n---------------------`n%a3%`n======================`n
Return

Headers(referer = "")
{
	Headers =
	( LTrim
		Referer: %referer%
		User-Agent: Opera/9.80 (Windows NT 5.1) Presto/2.12.388 Version/12.16
	)

	Return Headers
}

; WinHttpRequest.ahk
;
; Usage is similar to HTTPRequest (by VxE),
; Please visit the HTTPRequest page (http://goo.gl/CcnNOY) for more details.
;
; Supported Options:
; 	NO_AUTO_REDIRECT
; 	Timeout: <Seconds>
; 	Proxy: <IP:Port>
; 	Codepage: <CPnnn>	- e.g. "Codepage: 65001"
; 	Charset: <Encoding>	- e.g. "Charset: UTF-8"
; 	SaveAs: <FileName>
; Return:
; 	Success = -1, Timeout = 0, No response = Empty String
;
; How to clear cookie:
; 	WinHttpRequest( [] )
;
; ChangeLog:
; 	2015-4-25 - Added option "Method: HEAD"
; 	2014-9-7  - Fixed a bug in "Charset:"
; 	2014-7-11 - Fixed a bug in "Charset:"
WinHttpRequest( URL, ByRef In_POST__Out_Data="", ByRef In_Out_HEADERS="", Options="" )
{
	static nothing := ComObjError(0)
	static oHTTP   := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	static oADO    := ComObjCreate("adodb.stream")

	; Clear cookie
	If IsObject(URL)
		Return oHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")

	; POST or GET
	If (In_POST__Out_Data != "") || InStr(Options, "Method: POST")
		oHTTP.Open("POST", URL, True)
	Else If InStr(Options, "Method: HEAD")
		oHTTP.Open("HEAD", URL, True)
	Else
		oHTTP.Open("GET", URL, True)

	; HEADERS
	If In_Out_HEADERS
	{
		In_Out_HEADERS := Trim(In_Out_HEADERS, " `t`r`n")
		Loop, Parse, In_Out_HEADERS, `n, `r
		{
			If !( _pos := InStr(A_LoopField, ":") )
				Continue

			Header_Name  := SubStr(A_LoopField, 1, _pos-1)
			Header_Value := SubStr(A_LoopField, _pos+1)

			If (  Trim(Header_Value) != ""  )
				oHTTP.SetRequestHeader( Header_Name, Header_Value )
		}
	}

	If (In_POST__Out_Data != "") && !InStr(In_Out_HEADERS, "Content-Type:")
		oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")

	; Options
	If Options
	{
		Loop, Parse, Options, `n, `r
		{
			If ( _pos := InStr(A_LoopField, "Timeout:") )
				Timeout := SubStr(A_LoopField, _pos+8)
			Else If ( _pos := InStr(A_LoopField, "Proxy:") )
				oHTTP.SetProxy( 2, SubStr(A_LoopField, _pos+6) )
			Else If ( _pos := InStr(A_LoopField, "Codepage:") )
				oHTTP.Option(2) := SubStr(A_LoopField, _pos+9)
		}

		oHTTP.Option(6) := InStr(Options, "NO_AUTO_REDIRECT") ? 0 : 1
	}

	; Send...
	oHTTP.Send(In_POST__Out_Data)
	ReturnCode := oHTTP.WaitForResponse(Timeout ? Timeout : -1)

	; Handle "SaveAs:" and "Charset:"
	If InStr(Options, "SaveAs:")
	{
		RegExMatch(Options, "i)SaveAs:[ \t]*\K[^\r\n]+", SavePath)

		oADO.Type := 1
		oADO.Open()
		oADO.Write( oHTTP.ResponseBody )
		oADO.SaveToFile( SavePath, 2 )
		oADO.Close()

		In_POST__Out_Data := ""
	}
	Else If InStr(Options, "Charset:")
	{
		RegExMatch(Options, "i)Charset:[ \t]*\K[\w-]+", Encoding)

		oADO.Type     := 1
		oADO.Mode     := 3
		oADO.Open()
		oADO.Write( oHTTP.ResponseBody() )
		oADO.Position := 0
		oADO.Type     := 2
		oADO.Charset  := Encoding
		In_POST__Out_Data := IsByRef(In_POST__Out_Data) ? oADO.ReadText() : ""
		oADO.Close()
	}
	Else
		In_POST__Out_Data := IsByRef(In_POST__Out_Data) ? oHTTP.ResponseText : ""

	; output headers
	In_Out_HEADERS := "HTTP/1.1 " oHTTP.Status() "`n" oHTTP.GetAllResponseHeaders()

	Return, ReturnCode ; Success = -1, Timeout = 0, No response = Empty String
}
;===============================================================

Post Reply

Return to “Ask for Help (v1)”