如何终止在C#中的HttpWebRequest连接?它不工作,即使设置超时或readwritetimeout [英] How to terminate HttpWebRequest Connection in C#?It doesn't work even set timeout or readwritetimeout

查看:3604
本文介绍了如何终止在C#中的HttpWebRequest连接?它不工作,即使设置超时或readwritetimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,当它的时间太长的时间连接到终止的HttpWebRequest。
这里只是一个simaple代码,我写的:

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();
            }



但是,有时连接将采取有关15分钟获得响应。
的情况15分钟后,我仍然可以得到响应,但不是URL的完整源代码。
我猜测,连接速度太慢,该URL响应我的超时时间内一个比特的数据,只是说,例如接收1个字节的5秒,所以它不会过期timoue但它是非常长的。
我怎样才能终止连接?
感谢:)

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:)

推荐答案

您可能会发现超时实际工作,而是试图关闭时,危在旦夕流。我不知道为什么会发生,但有时它。我从来没有使用 ReadToEnd的,但我碰到这个使用运行时

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 using Read.

我通过调用中止上的要求,我才关闭流解决了这一问题。这是一个有点克鲁格的,但它是有效的。 。下面的缩写代码显示技术

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.

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();
}



什么我发现是, ReadTimeout ReadWriteTimeout 如预期的工作。也就是说,当读超时,执行确实转到最后块。而如果 request.Abort 不存在,调用 sr.Dispose 将挂起。

What I've found is that the ReadTimeout and ReadWriteTimeout work as expected. That is, when the read times out, execution really does go to the finally block. And if the request.Abort isn't there, the call to sr.Dispose will hang.

这篇关于如何终止在C#中的HttpWebRequest连接?它不工作,即使设置超时或readwritetimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆