I want to terminate a httpwebrequest when it takes too long time in connection. Here is just a simaple code that I wrote:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Timeout = 5000;
request.ReadWriteTimeout = 5000;
request.Proxy = new WebProxy("http://" + proxyUsed + "/", true);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.01; Windows NT 5.0)";
using (WebResponse myResponse = request.GetResponse())
{
using (Stream s = myResponse.GetResponseStream())
{
s.ReadTimeout = 5000;
s.WriteTimeout = 5000;
using (StreamReader sr = new StreamReader(s, System.Text.Encoding.UTF8))
{
result = sr.ReadToEnd();
httpLink = myResponse.ResponseUri.AbsoluteUri;
sr.Close();
}
s.Close();
}
myResponse.Close();
}
However, sometimes the connection will take a about 15minutes to get the response. The situation is after 15minutes I still can get the response but not the full source code of the URL. I guess that the connection is too slow that the URL response me a bit data within the timeout, just say for example receive 1byte in 5seconds, so it doesn't expire the timoue but it's very long. How can I terminate the connection? Thanks:)
You might find that the timeout is actually working, but the thread hangs when trying to close the stream. I don't know why it happens, but sometimes it does. I've never used
ReadToEnd
, but I've run across this when usingRead
.I fixed the problem by calling
Abort
on the request, before I close the stream. It's a bit of a kluge, but it's effective. The abbreviated code below shows the technique.What I've found is that the
ReadTimeout
andReadWriteTimeout
work as expected. That is, when the read times out, execution really does go to thefinally
block. And if therequest.Abort
isn't there, the call tosr.Dispose
will hang.Make sure to also close the connection on exception. In that case the WebResponse object is on the WebException:
Break down the stream reading, and abort if the total time has been too long.