Hi All
Pardon me for being so naive.I have a task to check the status of a group of Urls (whether active or inactive) and return the status to the log file every 3-4 minutes. Following is the code that I used:
void cacheTimer_Elapsed(object sender, ElapsedEventArgs e)
{
serviceTimer.Enabled = false;
try
{
string[] lstOfUrls = new string[] { "http://www.policybazaar.com", "http://www.comparebima.com", "http://www.accuratequotes.in" };
foreach (string s in lstOfUrls)
{
try
{
WebRequest request = HttpWebRequest.Create(s);
request.Timeout = 10000;
request.Method = "HEAD";
HttpWebResponse resp = request.GetResponse() as HttpWebResponse;
response.StatusCode == HttpStatusCode.OK
if (statusCode >= 100 && statusCode < 400)
{
Library.WriteErrorLog("Url {0} is working: ",s);
}
else if (statusCode >= 500 && statusCode <= 510)
{
Library.WriteErrorLog("The remote server has thrown an internal error. Following Url is not valid: {0}", s);
}
}
catch (Exception ex)
{
{
Library.WriteErrorLog("Could not test url {0}.", s);
}
}
}
}
catch (Exception ex)
{
string str=ex.Message;
}
finally
{
serviceTimer.Enabled = true;
}
}
protected override void OnStop()
{
serviceTimer = null;
}
}
}
This ,obviously is not working for me,It is giving some other exception at all times in the log file:
"28-02-2014 13:47:34:System;The remote server returned an error: (500) Internal Server Error."
while the links are working.
I used status codes,let me know if it could have been done in an easier way.
Also,someone suggested I remove if blocks from the program and replace them with a single condition(response.StatusCode == HttpStatusCode.OK) which I did and use URI instead of array of URLs .Dont know how that is going to be done .Please share your views on that and ,if possible,kindly share a code snippet for my requirement.I have to do this using windows service and generate a log of website status by hitting the URLs every 5 minutes and logging the activity every 5 minutes.
For e.g. : "policybazaar.com is down at the moment,comparebima.com is Up,accuratequotes.in is down"
Thanks
Sumit