Website Monitoring Code written in C# .NET

-------------------------------------------------------
Goto blog home
Visit my new blog dedicated to Internet of Things, Embedded Programming & Automation
-------------------------------------------------------
Today I wrote an alerting tool. The tool would sit as a NotifyIcon on your system tray, and keep monitoring a website to find whether it went down. I will devote writing a windows application to run and be controlled from the system tray at a later post. This post concentrates on just the web URL monitoring.

public bool IsWebSiteDown(string strURL)
{
bool _errorCondition = false;
string _strContent = String.Empty;
StreamReader _objSR;
WebResponse _objResponse = null;
WebRequest _objRequest = HttpWebRequest.Create(strURL);
string _strSource = strServiceIdentifier;
int _contentLength = 0;
try
{
_objResponse = _objRequest.GetResponse();
_objSR = new StreamReader(_objResponse.GetResponseStream(), Encoding.ASCII);
_strContent = _objSR.ReadToEnd();
_contentLength =_strContent.Length;
_strSource = _contentLength.ToString();
_objSR.Close();
_objResponse.Close();
}
catch(Exception e)
{
_errorCondition = true;
}
return _errorCondition;
}

No comments:

Post a Comment