Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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) //Good requests
                        {
                            Library.WriteErrorLog("Url {0} is working: ",s);

                        }
                        else if (statusCode >= 500 && statusCode <= 510) //Server Errors
                        {
                            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
Posted
Updated 3-Mar-14 0:18am
v2

Try this code:

C#
List<string> urls = new List<string>();
            urls.Add("Add_URL_here");

            urls.ForEach(x => {
                WebRequest request = WebRequest.Create(x);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response == null || response.StatusCode != HttpStatusCode.OK) { 
                // Log that website is down
                }

                response.Close();
            });</string></string>
 
Share this answer
 
Thanks for replying D@nish,

Now ,I am using the following code:

void cacheTimer_Elapsed(object sender, ElapsedEventArgs e)
{
serviceTimer.Enabled = false;

List<string> lstOfUrls = new List<string>();
lstOfUrls.Add("http://www.policybazaar.com");
lstOfUrls.Add("http://www.comparebima.com");
lstOfUrls.Add("http://www.accuratequotes.in");

lstOfUrls.ForEach((s)=>
{
WebRequest request = WebRequest.Create(s);
EventLog.WriteEntry("NotifyWService", "Web request sent " + DateTime.Now);
//request.Timeout = 30000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
EventLog.WriteEntry("NotifyWService", "Web response acquired " + DateTime.Now);

if (response == null || response.StatusCode != HttpStatusCode.OK)
{
EventLog.WriteEntry("NotifyWService", "if block for inactive status " + DateTime.Now);
lib.StatusInactive(s);
EventLog.WriteEntry("NotifyWService", "writing inactive status after calling the StatusInactive() method " + DateTime.Now);
}

else
{
EventLog.WriteEntry("NotifyWService", "else block because site is working " + DateTime.Now);
lib.StatusActive(s);
EventLog.WriteEntry("NotifyWService", "writing active status after calling the StatusActive() method " + DateTime.Now);
}
EventLog.WriteEntry("NotifyWService", "Out of if else block " + DateTime.Now);
response.Close();
EventLog.WriteEntry("NotifyWService", "response has been closed" + DateTime.Now);
});

}

Now the issue is that on checking the Eventlog ,the control is not going beyond the web request and it is not able to get the response.On checking the eventlog , the last step where the control reaches is :
EventLog.WriteEntry("NotifyWService", "Web request sent " + DateTime.Now);

Any idea what am I doing wrong because the control is not going any further then the request.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900