You may find that the timeout does work, but the thread freezes when you try to close the stream. I do not know why this happens, but sometimes it happens. I never used ReadToEnd
, but I used this when using Read
.
I fixed the problem by calling Abort
on request before I close the stream. It is a little kluge, but it is effective. The abbreviated code below shows the technique.
HttpWebResponse response = null; StreamReader sr = null; try { response = (HttpWebResponse)request.GetResponse(...); Stream s = response.GetResponseStream(); sr = new StreamReader(s, Encoding.UTF8); // do your reading here } finally { request.Abort(); // !! Yes, abort the request if (sr != null) sr.Dispose(); if (response != null) response.Close(); }
I found that ReadTimeout
and ReadWriteTimeout
work as expected. That is, when the reading time ends, the execution really goes into the finally
block. And if request.Abort
does not exist, the sr.Dispose
call will hang.
source share