简体   繁体   English

.NET-请求在特定时间段后超时

[英].NET - Requests timing out after certain period of time

I'm writing a simple application to query a certain URL over a period of time to detect changes, but I notice that after 5,000 requests or so, the requests will start to time out. 我正在编写一个简单的应用程序,以便在一段时间内查询某个URL以检测更改,但是我注意到在5,000个左右的请求之后,这些请求将开始超时。

It does cycle through hosts (has about 200 different hosts to cycle through) 它确实在主机之间循环(有大约200个不同的主机在循环)

The target and source connections are both extremely stable. 目标和源连接都非常稳定。

After restarting the program, there are no issues with timing out. 重新启动程序后,超时没有问题。

Here is the relevant code: 以下是相关代码:

public static string download_data()
{
    String ret = String.Empty;
    try
    {
        Uri newUri = new Uri(FinalURL.Replace(Host,Hosts[bestHostIndex]));
        var request = (HttpWebRequest)WebRequest.Create(newUri);
        request.Method = "GET";
        request.AllowAutoRedirect = true;
        request.KeepAlive = true;
        request.Referer = EscapedURL;
        request.UserAgent = UA;
        request.Headers.Add("Pragma", "no-cache");
        request.Timeout = 3000;
        request.Host = HostMain;
        request.Proxy = null;
        request.CookieContainer = Session.Cookies;
        using (var resp = request.GetResponse())
        {
            var reader = new StreamReader(resp.GetResponseStream());
            ret = reader.ReadToEnd();
        }

            return ret;
    }
    catch(WebException e)
    {
        Console.WriteLine("Error: "+e.Message);
        if (e.Status == WebExceptionStatus.ProtocolError)
        {
            WebResponse resp = e.Response;
            using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
            {
                ret = sr.ReadToEnd();
            }
        }
        return ret;
    }
}

Very strange because I think for there to be an issue with connectivity, then restarting the application wouldn't fix the issue. 非常奇怪,因为我认为连接存在问题,然后重新启动应用程序无法解决问题。 It seems like the network is being "jammed" the longer the application runs. 应用程序运行的时间越长,网络似乎越被“阻塞”。 Perhaps I'm not properly flushing the resource or something like that. 也许我没有正确地刷新资源或类似的东西。

Also it may be relevant to mention that the first request ALWAYS times out. 同样有必要提及的是,第一个请求总是超时。

Help would be great 帮助会很棒

You need couple more using statements: 您还需要using语句:

using (var resp = request.GetResponse())
using (var responseStream = resp.GetResponseStream())
using (var reader = new StreamReader(responseStream))
{
    ret = reader.ReadToEnd();
}

Without calling Close or disposing the stream returned by GetResponseStream the connection won't be released, and that's why you're running out of connections after a while. 如果不调用Close或处理GetResponseStream返回的流,则连接不会被释放,这就是为什么一段时间后连接用尽的原因。 MSDN is clear about that: MSDN对此很清楚:

You must call either the Stream.Close or the HttpWebResponse.Close method to close the stream and release the connection for reuse. 您必须调用Stream.CloseHttpWebResponse.Close方法来关闭流并释放连接以供重用。 It is not necessary to call both Stream.Close and HttpWebResponse.Close , but doing so does not cause an error. 不必同时调用Stream.CloseHttpWebResponse.Close ,但是这样做不会导致错误。 Failure to close the stream will cause your application to run out of connections. 无法关闭流将导致您的应用程序用尽连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM