an isWebSiteRunning routine

Have you ever wanted to know if your website is down? I’m sure there are services out there that will report to you if your website is not running but you can also do it yourself.

Here is one way to do it that I tried and tested and although my testing was not exhaustive, it seems to work at least for my own website that I tested running locally.  So how to check if your web site is up and operational? As in the prior posts, I started with a simple class, then I created a console program, referenced the class, instantiated the class and tested it out. The bulk of the work is done by the System.Net library that comes with Visual Studio 2013 and other versions of Visual Studio.

If a bad status code is present or if any error comes up, it should indicate the website is not running. I’ve not tested this extensively, but in my handful of tests it did tell me when my site was down.

The other thing I though might be useful was to include a property to be used to describe if the site was running as a Boolean data type.

Here is the namespace isWebSiteRunning and the check site class  ….

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

namespace isWebSiteRunning
{
    public class checkSite
    {
        private bool _isRunning=false;

        public bool isRunning { get { return _isRunning; }  }

        public checkSite(string sURL)
        {
            try { 
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sURL);

                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            
                if (response == null || response.StatusCode != HttpStatusCode.OK)   
                {
                    _isRunning = false;

                } 
                else
                {
                    _isRunning = true;
                }
            
                response.Close();
                
                }

            catch (Exception ex)
            {
                
                Console.WriteLine(ex.Message.ToString());                
            }

            finally
            {
                Console.WriteLine("site checking is completed");
            }

        }
        
    }
}

Next, the console program used to test out if the web site is up and running.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test_isWebSiteRunning
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("start");
            isWebSiteRunning.checkSite cs = new isWebSiteRunning.checkSite("https://custom-designed-databases.com");
            if (cs.isRunning) { Console.WriteLine("site is running"); } else { Console.WriteLine("site is NOt running"); }
            Console.WriteLine("done, press any key");
            Console.Read();
        }
    }
}

 

Note to make it easier to read the output, I put in the following line of C# code;

Console.WriteLine(“done, press any key”);

That statement above will pause the program so it will not close. This pause will allow the user to read any output.

So what is the output when the console program to test if the web site is running?

The first example shows when the site is running normally.

site_is_running console program

site_is_running console program

If the site is down, the following output is displayed:

SiteIsNotRunning console program

SiteIsNotRunning console program

Like the prior posts, these classes and console programs are great ways to demonstrate small programs that are useful and might be called frequently on a schedule of some kind. In a future post we will try to put some of these under the control of a program that periodically runs on a schedule.

 

Posted in C#