WebRequest.GetResponse ignores my timeout value?

3.7k views Asked by At

I'm trying to set a near-infinite timeout value on a WebRequest, considering that the HTTP-based API which I'm querying already graciously handles timeouts by sending back an XML document that declares a timeout. For some reason though, the following code still throws an exception:

Dim request As WebRequest = WebRequest.Create("http://api/cgi-bin/do?cmd=longRunningOperation&timeout=300")
' ^ command returns timeout if not complete within 300 seconds (5 minutes)

request.Timeout = Integer.MaxValue ' so we don't need client-side timeout handling

Dim response As WebResponse = request.GetResponse() ' yet this call blows up

I'm sorry that I can't provide something that you can debug, but according to MSDN, this is how to do it. Yet I still get a WebException after about 100 seconds (message: The operation has timed out, status: WebExceptionStatus.Timeout).

Can someone explain this behaviour? (And if not, propose a workaround?)

EDIT

The problem probably has something to do with the fact that this snippet is executed in a Task, but so far that's still all I know.

EDIT2

Running the code on the UI thread still throws the same exception, so maybe that's not it after all...

EDIT3

After a good night's sleep, I think it may be doing this because I'm not properly closing my response or response streams. I'm sending multiple requests while the long-running operation is still busy. I can imagine that there is a gotcha on a lower level if you don't properly dispose here. But for me it's hard to say because I don't know the inner workings of the webrequest/webresponse classes.

The code that I'm using: http://code.google.com/p/dune-api-codepack/source/browse/ApiWrappers/DuneCommands/CommandResult.vb (not the latest commit but the differences are not too big)

The problematic method is GetResults(command). Don't mind the GetResultsAsync method, it is just a draft (although it probably contains the same error).

EDIT4

Disregard everything I've said, the problem and solution are in my answer below.

1

There are 1 answers

0
Steven Liekens On BEST ANSWER

Special thanks @mellamokb for trying to figure it out with me, but the solution was actually something none of you could've known.

I was stupid enough to write post data to the requeststream before setting the request timeout. I guess that's what you get for trying to write code when you're tired.

Thanks again for your time.