Click here to Skip to main content
15,868,065 members
Articles / Web Development / HTML
Tip/Trick

Simple Demo to Check Status of API Server

,
Rate me:
Please Sign up or sign in to vote.
4.56/5 (5 votes)
25 Mar 2015CPOL2 min read 24.3K   11   7
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Doctorandin Technische Universität Berlin
Iran (Islamic Republic of) Iran (Islamic Republic of)
I have been working with different technologies and data more than 10 years.
I`d like to challenge with complex problem, then make it easy for using everyone. This is the best joy.

ICT Master in Norway 2013
Doctorandin at Technische Universität Berlin in Data Scientist ( currently )
-------------------------------------------------------------
Diamond is nothing except the pieces of the coal which have continued their activities finally they have become Diamond.

http://www.repocomp.com/

Written By
Chief Technology Officer
India India
Learning never ends.

Comments and Discussions

 
QuestionNice Sample Pin
Member 1156090827-Mar-15 5:36
Member 1156090827-Mar-15 5:36 
AnswerRe: Nice Sample Pin
Gaurav Aroraa27-Mar-15 7:54
professionalGaurav Aroraa27-Mar-15 7:54 
QuestionMy vote +4 Pin
Shuby Arora26-Mar-15 11:28
Shuby Arora26-Mar-15 11:28 
AnswerRe: My vote +4 Pin
Gaurav Aroraa26-Mar-15 23:40
professionalGaurav Aroraa26-Mar-15 23:40 
QuestionRight idea, but Oversimplified Pin
Yvan Rodrigues25-Mar-15 14:20
professionalYvan Rodrigues25-Mar-15 14:20 
AnswerRe: Right idea, but Oversimplified Pin
Gaurav Aroraa26-Mar-15 2:09
professionalGaurav Aroraa26-Mar-15 2:09 
AnswerRe: Right idea, but Oversimplified Pin
Shuby Arora26-Mar-15 11:29
Shuby Arora26-Mar-15 11:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.