1

I have the function to check if website is available.

    public bool ConnectionAvailable(string strServer)
    {
        try
        {
            HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create(strServer);
            reqFP.Timeout = 10000;
            HttpWebResponse rspFP = (HttpWebResponse)reqFP.GetResponse();

            if (HttpStatusCode.OK == rspFP.StatusCode)
            {
                // HTTP = 200 - Internet connection available, server online
                rspFP.Close();
                return true;
            }
            else
            {
                // Other status - Server or connection not available
                rspFP.Close();
                return false;
            }
        }
        catch (WebException)
        {
            // Exception - connection not available
            return false;
        }
    }

It's not mine code. I found it in the Net.

The problem is when some website isn't available. I want to wait x miliseconds (set in reqFP.Timeout), then function should return false. But everytime I have to wait ~20 seconds (even if i set 10 seconds in "timeout").

Do you have any idea what is wrong?

PS: Sorry for language mistakes.

Kuba
  • 11
  • 1
  • 2

2 Answers2

3

From MSDN article:

A Domain Name System (DNS) query may take up to 15 seconds to return or time out. If your request contains a host name that requires resolution and you set Timeout to a value less than 15 seconds, it may take 15 seconds or more before a WebException is thrown to indicate a timeout on your request.

If it's possible that's the case? Try the sane code but using IP address instead of hostname. Also, when you get false after waiting 20 seconds, are you sure it's because of timeout and not because the server returned something other than "200"?

Dyppl
  • 12,161
  • 9
  • 47
  • 68
  • Thank you for reply. I tried also with ip adress instead of hostname and the result is the same. But... I check if false is returning from "else" section, and it isn't. False is returning beacuse of exception "Limit of time of operation has expired". – Kuba Jun 09 '11 at 15:32
  • When I tried to check f.eg. http://localhost/a.html when the server was on (but a.html doesn't exist) I got answer (404 error) immediately. But when I tried to check the same adress when the server was off I had to wait ~20 seconds and I got exception "Limit of the time...". – Kuba Jun 09 '11 at 15:36
  • @Kuba: take a look at this question: http://stackoverflow.com/questions/1500955/adjusting-httpwebrequest-connection-timeout-in-c – Dyppl Jun 09 '11 at 15:58
0

Try this property: ReadWriteTimeout

xling
  • 252
  • 1
  • 9