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:
- I raise my server
- I successfully connect to it, perform a POST, and close the request / response stream
- I kill the server
- 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.