Website Monitoring - Simple Website Monitor

17. April 2008 01:44

Recently, we needed a way to monitor if our Company's website was up and running. We didn't care if the server was up; we wanted to know if users could hit our site. We brainstormed for about 2 minutes and came up with this solution.

First we created an HttpWebRequest:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("website URL goes here");

Next, we try to get a response from it...
try
{
  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  label1.text = "Status is: " + response.StatusCode.ToString();
  response.Close(); //necessary, otherwise response will hang after 2nd attempt
}
catch (WebException webError)
{
  label1.Text = "Error! Message: " + webError.Message.ToString();
}


If there is an error, the catch will fire and let us know the problem.
Now, what if you want the code to check this every so often? What's the point of checking it once if the site goes down immediately afterward, right? You can easily solve this issue by using a timer.

Create your timer...
System.Timers.Timer tmrCheck = new System.Timers.Timer();
tmrCheck.Elapsed += new ElapsedEventHandler(checkResponse);
tmrCheck.Interval = 5000; //will check every 5 seconds
trmCheck.Enabled = true;


Every time you fire the event, you are essentially creating a thread and your labels will no longer be available for updating. You'll need to create some delegates to handle this.
public delegate void updLabel(string strResponse);
public delegate void updLabelError(string strError);


You'll now need to create two methods, checkReponse() and updLabel().  Upon completion of 5 seconds, your timer will fire the checkResponse(). Depending on the result of the response, updLabel() will write out the status of the target site.
private void checkResponse()
{
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.truwest.org");
 string strMessage = "";

 try
 {
   HttpWebResponse response = (HttpWebResponse)request.GetResponse();
   strMessage = "Status is: " + response.StatusCode.ToString();
   lblReponse.Invoke(new updLabel(this.showMessage), strMessage);
   response.Close(); //necessary, otherwise response will hang after 2nd attempt
 }
 catch (WebException webError)
 {
   strMessage = "Error! Message: " + webError.Message.ToString();
   lblReponse.Invoke(new updLabel(this.showMessage), strMessage);
 }
}

private void showMessage(string strResponse)
{
  label1.Text = strResponse;
}


Note the response.Close() event. I only bring it up because during testing, the progam would hang after the second attempt to check the site. Using this event cleared up the problem.

Hope this helps and let us know if you encounter any problems with the exemplary code. The end.

Tags: , , , , , ,

ASP.NET | C#

blog comments powered by Disqus



My Random Thought

I think the OCW is a great thing to have available to those who are in school, just finished school or just want to educate themself

http://ocwconsortium.org/

John On Twitter

Discounts