How to check programmatically if a website is running

Recently, I got a request from one of my partners to check if a particular website is running or not and this has to be done from a windows service; which should be checking automatically at a given interval once started.

Well, you can use WebRequest and WebResponse Classes (under namespace System.Net) to first create a request object for the particular website and then use the GetResponse() function to check if any response has been sent from the web site.

More information about the WebRequest and WebResponse classes can be found here:

https://msdn.microsoft.com/en-us/library/system.net.webresponse.aspx

Here is the code snippet for the windows service which checks every 1 min for a particular website running status:

----------------------------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;
using System.Timers;
using System.Net;

namespace DemoWinService
{
    partial class CheckWebApp : ServiceBase
    {
        TimerCallback objTimerCallBack;
        System.Threading.Timer objStartTimer;

        public CheckWebApp()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            StartChecking();
        }

        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
        }
        private void StartChecking()
        {
            objTimerCallBack += new TimerCallback(WebSiteCheck);
            objStartTimer = new System.Threading.Timer(objTimerCallBack, null,0,60000);
        }

        private void WebSiteCheck(object obj)
        {
            WebRequest objRequest = WebRequest.Create("https://www.contoso.com");

            WebResponse objResponse = objRequest.GetResponse();
            if(objResponse.ContentLength > 0)
                EventLog.WriteEntry("Success->" + objResponse.ContentLength.ToString());
            else
                EventLog.WriteEntry("Website is not running.");
            objResponse.Close();
        }
    }
}

----------------------------------------------------------------------------------------------------

However, while installing (many times) the windows service I got installation errors; I checked in the “services windows” and found that the my service was not fully uninstalled and when tried to change the properties, I got an error “The specified service has been marked for deletion.” To overcome that error you can try closing the services windows and reopen it. If that doesn’t work then try rebooting the machine.

You can however, use HttpWebRequest and HttpWebResponse classes to check the status of the website if it is running successfully or not by checking the server status (no bad request or server error etc):

The WebSiteCheck function will look like this

---------------------------------------

 private void WebSiteCheck(object obj)
        {
            HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("https://www.contoso.com");

            HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();

            if ((objResponse.StatusCode == HttpStatusCode.OK) && (objResponse.ContentLength > 0))
                EventLog.WriteEntry("The length of the data is " + objResponse.ContentLength.ToString() + " and the status code is " + objResponse.StatusCode.ToString());
            else
                EventLog.WriteEntry("Website is not running.");

            objResponse.Close();
        } 

----------------------------------

Happy Programming!