Make WinHttp.WaitForResponse() to wait more than 30 seconds

Put simple Tips and Tricks that are not entire Tutorials in this forum
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Make WinHttp.WaitForResponse() to wait more than 30 seconds

Post by tmplinshi » 08 Aug 2015, 23:06

By default, the ReceiveTimeout of WinHttp.WinHttpRequest.5.1 is 30 seconds! Which means...
  • WinHttp.WaitForResponse(-1) ; Wait forever? No, 30 seconds max.
  • WinHttp.WaitForResponse(60) ; Wait 60 seconds? No, 30 seconds max.
To wait for more than 30 seconds, you'll need to change the default timeout, by using WinHttp.SetTimeouts(ResolveTimeout, ConnectTimeout, SendTimeout, ReceiveTimeout)

For example, modify ReceiveTimeout to 120 seconds:
WinHttp.SetTimeouts(0, 60000, 30000, 120000)
The first 3 values are the default values, see IWinHttpRequest::SetTimeouts method.

[code]whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", URL, true)
whr.SetTimeouts(0, 60000, 30000, 120000)
whr.Send()
whr.WaitForResponse(90)[/code]


Edit: SetTimeouts should be set before open.

Code: Select all

whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.SetTimeouts(0, 60000, 30000, 120000)
whr.Open("GET", URL, true)
whr.Send()
whr.WaitForResponse(90)
Hope this post helps someone.
Last edited by tmplinshi on 20 Feb 2019, 23:34, edited 1 time in total.

gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: Make WinHttp.WaitForResponse() to wait more than 30 seconds

Post by gilliduck » 09 Aug 2015, 16:07

Where are you getting that? All the documentation I've been able to hastily Google is showing that it defaults to infinite and you have to set a value. This is just random Googling though, I'm not personally familiar with this.

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Make WinHttp.WaitForResponse() to wait more than 30 seconds

Post by tmplinshi » 09 Aug 2015, 20:40

From real testing. Try this example:

Without SetTimeouts:

Code: Select all

startTime := A_TickCount
ToolTip, (Without SetTimeouts) waiting...

ComObjError(0)
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "http://tmplinshi.sinaapp.com/test/timeout-test.php", true)
whr.Send()
whr.WaitForResponse()

MsgBox, % "Without SetTimeouts`n" (A_TickCount-StartTime)/1000 "`n" whr.ResponseText
Modify ReceiveTimeout to 120 seconds:

Code: Select all

startTime := A_TickCount
ToolTip, (Modify ReceiveTimeout to 120 seconds) waiting...

ComObjError(0)
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "http://tmplinshi.sinaapp.com/test/timeout-test.php", true)
whr.SetTimeouts(0, 60000, 30000, 120000)
whr.Send()
whr.WaitForResponse()

MsgBox, % "Modify ReceiveTimeout to 120 seconds`n" (A_TickCount-StartTime)/1000 "`n" whr.ResponseText
The code of timeout-test.php:

Code: Select all

<?php
sleep(40)
?>This content took 40s to deliver.  Congratulations, you didn't time out!
Last edited by tmplinshi on 09 Aug 2015, 21:59, edited 2 times in total.

gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: Make WinHttp.WaitForResponse() to wait more than 30 seconds

Post by gilliduck » 09 Aug 2015, 20:45

After 40 seconds I get the message box with both scripts.

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Make WinHttp.WaitForResponse() to wait more than 30 seconds

Post by tmplinshi » 09 Aug 2015, 21:03

:shock: Without SetTimeouts, I can't wait more than 30 seconds Every time... Are you sure you wasn't running the second code twice? :|

gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: Make WinHttp.WaitForResponse() to wait more than 30 seconds

Post by gilliduck » 09 Aug 2015, 21:12

Just retested, took just a hair over 40 seconds each time regardless of which one I was using. Something on your end?

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Make WinHttp.WaitForResponse() to wait more than 30 seconds

Post by tmplinshi » 09 Aug 2015, 21:17

Thanks for your testing. What I got is, the first code timeout at about 30 seconds (29.9999 or 30.xxx) each time.
Is it possible that your OS has fixed this? Windows 10? I'm using win7 x64.

gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: Make WinHttp.WaitForResponse() to wait more than 30 seconds

Post by gilliduck » 09 Aug 2015, 21:26

Win 7 x32

Make a version where the timeout is 20 seconds, I'm kinda curious as to what'll happen.


gilliduck
Posts: 265
Joined: 06 Oct 2014, 15:01

Re: Make WinHttp.WaitForResponse() to wait more than 30 seconds

Post by gilliduck » 09 Aug 2015, 21:42

It seems to behave as I'd expect. If I set at timeout below the timeout-test sites known timeout time (sorry, that's a lot of uses of the word time) then it errors. If I set it after that, or don't set it, it works fine. If you're getting a consistent 30 seconds, I'm guessing it's something on your end. It seems to work exactly as the documentation supports.

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Make WinHttp.WaitForResponse() to wait more than 30 seconds

Post by tmplinshi » 09 Aug 2015, 21:58

Thanks again gilliduck. I believe your computer is not normal... :crazy:

Post Reply

Return to “Tips and Tricks (v1)”