开发者

I keep getting "Timed Out" Errors on C# WebRequest

I've created a program that loads data from an HTTP request. It has to load it every 1 sec or 0.25 sec.

However I've found that that many of my request - a few every minute - are timed out. It's weird because trying to reload the same address in a web browser, even when reloading very often, I always get the contents开发者_如何学C quickly.

This is the code I use:

        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
        myRequest.Method = "GET";
        myRequest.Timeout = 1500;
        myRequest.KeepAlive = false;
        myRequest.ContentType = "text/xml";
        WebResponse myResponse;
        try
        {
            myResponse = myRequest.GetResponse();
        }
        catch (Exception e)
        {
            return e.Message;
        }
        StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
        string result = sr.ReadToEnd();
        sr.Close();
        myResponse.Close();

And the error I get is:

The operation has timed out

I've tried with both keepalive set to true and false.

So the question is - is there any way to reduce the amount of timeouts? Every request I request is crucial. I'm pretty sure it's relevant to the fact that I query the same address often, but surely there's a way to prevent it...?

Thanks a lot!


There are two kind of timeouts. Client timeout and server timeout. Here you get server timeout which is probably due to a security defense mechanism (IDS/IPS) from web server as it recognizes the pattern (large amount of requests from a single IP in a small period of time) as a DoS (Denial of Service) attack. Then your IP is banned for a period of time. Increasing client timeout as you did in your code is not useful at this case. @Comment: Turning off KeepAlive makes you application create new connection to web server on each request. Maybe this is the big difference between your code and running the javascript from the browser. Set KeepAlive to true.

App.Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<connectionManagement>
<add address = "*" maxconnection = "1000" />
</connectionManagement>
</system.net>
</configuration>


Try setting the timeout to Timeout.Infinite:

    var request = WebRequest.Create(url);
        request.Proxy = null;
        request.Timeout = Timeout.Infinite;
        string text = "";
        using (var response = (HttpWebResponse)request.GetResponse())
        {
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                text = sr.ReadToEnd();
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜