Maximum time for a request with HttpWebRequest/Invoke-WebRequest?

Viewed 1804

I have fiddled around with both System.Net.HttpWebRequest and Invoke-WebRequest (I know that beneath it is the same code) and both gives a 500 status in the following scenario:

$response = Invoke-WebRequest 'https://httpstat.us/200?sleep=270000' -TimeoutSec 360000

How can this be? httpstat.us is clearly set to only wait for 4.5 min. and the timeout is way above that, but all I get is a:

Invoke-WebRequest : 500 - The request timed out. The web server failed to respond within the specified time. At line:1 char:13 + $response = Invoke-WebRequest 'https://httpstat.us/200?sleep=270000' ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc eption + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

It seems to me, that there's a maximum timeout of the request (about 3-4 min.). Anobody knows what to do?

I have set up this scenario because I have a website that I need to test that has a long response time.

1 Answers

Blogrbeard has provided the crucial pointer in a comment on the question:

It is https://httpstat.us itself that is unexpectedly timing out, because, as of this writing, the actual max. sleep time appears to be 3 mins. and 50 seconds (230 secs., i.e., in ms., as part of the query string: ?sleep=230000), not the 5 mins. documented.
?sleep=270000 - 4.5 mins. - is therefore too high a value.

Therefore, Invoke-WebRequest is blameless here - its -TimeoutSecs value never comes into play, and even omitting -TimeoutSecs altogether (i.e., specifying no timeout at all) would yield the same result:

PS> (Measure-Command { 
      Invoke-WebRequest https://httpstat.us/200?sleep=270000  2>&1 | Out-Default 
     }).TotalSeconds

Invoke-WebRequest : 500 - The request timed out. ...
...
230.57306

Update: As it turns out, it is Azure - where https://httpstat.usis being hosted as of this writing - that is imposing the 230-second limit:

When using the hosted instance the timeout is actually 230 seconds, which is the max timeout allowed by an Azure WebApp (see this post). If you host it yourself in IIS/IIS Express you won't have that limit.

Related