views:

40

answers:

1

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 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!

A: 

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>
Xaqron
Thanks for the answer, it makes sense - however, the same server I request the data from, has a very similar Javascript method that does exactly the same thing on the web page. It loads the same XML every 1 second. The javascript never seem to fail though..
BenB
What we are sure at this point is your request doesn't get a response. Starting from your side, check your firewall, connection speed, ... If everything is OK from your side check the difference between your request and what that Javascript does. Turning off Keep alive is not a good idea since your app will try more connections then maybe adding "MaxConnection" to your app.config helps.
Xaqron