Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple Demo to Check Status of API Server

0.00/5 (No votes)
25 Mar 2015 1  
In this tip, we will create a small utility using C# which tells us whether our API server is up or down.

Introduction

In this tip, we will create a small utility using C# which tells us whether our API server is up or down.

Purpose

Our purpose is to check the API server, so that we can make a request for our resources. It is recommended that before making any request to server (API server in our case), we should make sure that our server is ready and up to handle our requests.

Throughout this tip, we will create a tiny utility to solve our purpose.

Requirements

So, what do we require? We just want to know the status of our Web API server.

Creation of Utility

Let's create a simple console app, which tells us about the status of our API server:

  • Open Visual Studio
  • Create a C# console application, name it ‘knowserverstatus

    New Project VS

  • Add the following method:
    C#
    public static void ServerStatusBy(string url)
            {
                Ping pingSender = new Ping();
                PingReply reply = pingSender.Send(url);
    
                Console.WriteLine("Status of Host: {0}", url);
    
                if (reply.Status == IPStatus.Success)
                {
                    Console.WriteLine("IP Address: {0}", reply.Address.ToString());
                    Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
                    Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                    Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
                    Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
                }
                else
                    Console.WriteLine(reply.Status);
       }

    Do not forget to add the following namespace:

    C#
    using System.Net.NetworkInformation;
  • Make a call from main:
    C#
    Console.Write("Enter server url:");
                var url = Console.ReadLine();
                Console.Clear();
                ServerStatusBy(url);
                Console.ReadLine();
  • Run and we will get the following output:

    Input for server

    Output of Ping

Elaboration of Code

Let's elaborate the above code, here PING sends an ICMP (Internet Control Message Protocol) request to the requested server host/url and waits to receive the response back. If the value of Status is success, it means our server is up and running.

Here PingReply returns all properties, described as:

Proeprty Description
Address Gets the address of Host
Buffer Gets the buffer of data received, it's an array of bytes
PingOption It handles how data is transmitted
RoundTrip Gets the number of milliseconds to send ICMP echo request
Status Gets the status of request. If not Success, it means Host is not up

Conclusion

In this tip, we discussed all about PingReply class and with the help of this class, we created a utility to know the status of our server.

History

  • 25th March, 2015: Initial version

Feedback

Feel free to leave any feedback on this tip; it is a pleasure to see your comments and vote about this code. If you have any questions, please do not hesitate to ask me here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here