Checking the status of a website in .NET.
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 to share
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 to share
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 to share