0

I've set both Timeout and ReadWriteTimeout of HttpWebRequest, and still GetRequestStream does not time out when trying to connect to a non-existent server. I've run into similar problems in the past when the DNS resolution was struck, but this time the server in question is running on my own machine (localhost) so this not relevant.

The scenario is this:

  1. I raise my server
  2. I successfully connect to it, perform a POST, and close the request / response stream
  3. I kill the server
  4. I try to connect again, and expect that I will hit the timeout I set

At this point the thread hangs. I tried setting KeepAlive to false, but that didn't help.

I thought about running the above scenario in a separate thread, and if it takes too long kill it myself - however this solution is very ugly and won't prevent resource leaks.

1 Answer 1

5

Try using BeginGetRequestStream and EndGetRequestStream instead of GetRequestStream. Set up a timer and call EndGetRequestStream prematurely after enough time has elapsed that you assume the request will hang. Obviously you must also call EndGetRequestStream in the delegate passed in to BeginGetRequestStream if you do process the request normally so there has to be some synchronization between the two threads to keep from calling EndGetRequestStream twice.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.