我想在连接时间过长时终止httpwebrequest.这是我写的一个simaple代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
但是,有时连接需要大约15分钟才能得到响应.情况是在15分钟之后我仍然可以获得响应,但不是URL的完整源代码.我猜这个连接太慢了,以至于URL在超时时间内响应了一些数据,比如说在5秒内接收1byte,所以它不会使timoue到期但是它很长.我该如何终止连接?谢谢:)