Checking the status of a website in .NET.

I need to create a .NET function that checks if a specific website is available. What's the best way to do this? I was going to just ping the site, but I wondered if there was a more accurate method.

Thank!

+2


source to share


4 answers


I always use this to check if websites are working correctly:



public bool IsWebsiteOnline(string url)
{
    try
    {
        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
        myReq.Timeout = 10000;

        using (HttpWebResponse response = (HttpWebResponse)myReq.GetResponse()) 
        {
           return response.StatusCode == HttpStatusCode.OK;
        }
    }
    catch
    {
        return false;
    }
}

      

+16


source


Never assume that the ability to ping a site gives you any information about its status other than that the window is enabled and going to respond to messages.



+1


source


Ping checks if the server is responding. However, part of the web application might not be accessible, db might be done, etc. Etc.

SO writing a request for HTTP data from a url and:

1) check that the status code response is ok

2) check that there is content (not a blank page)

* 3), if you really want to be smart, depending on the requirements, confirm the returned data, that is, compare a part of it for a specific row.

*, but with the minimum minimum, I suggest 1 and 2.

0


source


You can use Watin for small tasks and thus judge whether a site is working as expected .

Watin also allows functionality testing.
(Some code is stolen from their site)

public void SearchForWatiNOnGoogle()
{
 using (IE ie = new IE("http://www.google.com"))
 {
  ie.TextField(Find.ByName("q")).TypeText("WatiN");
  ie.Button(Find.ByName("btnG")).Click();

  Assert.IsTrue(ie.ContainsText("WatiN"));
 }
} 

      

0


source







All Articles