Click here to Skip to main content
13,766,476 members
Click here to Skip to main content
Add your own
alternative version

Stats

13.5K views
11 bookmarked
Posted 25 Mar 2015
Licenced CPOL

Simple Demo to Check Status of API Server

Rate this:
Please Sign up or sign in to vote.
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:
    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:

    using System.Net.NetworkInformation;
  • Make a call from main:
    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)

Share

About the Authors

Mahsa Hassankashi
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.

*Article of The Community Spotlight in Microsoft ASP.NET, Wednesday, February 11, 2015, www.asp.net
*Article of The Day in Microsoft ASP.NET Tuesday, February 3, 2015, www.asp.net/community/articles
*1 Jan 2015: CodeProject MVP 2015
*22 Mar 2014: Best Web Dev Article of February 2014 - Second Prize


Gaurav Aroraa
Architect
India India
There is no age limit for learning and its never late, to start.

You may also be interested in...

Comments and Discussions

 
QuestionNice Sample Pin
Member 1156090827-Mar-15 6:36
memberMember 1156090827-Mar-15 6:36 
AnswerRe: Nice Sample Pin
Gaurav Aroraa27-Mar-15 8:54
professionalGaurav Aroraa27-Mar-15 8:54 
QuestionMy vote +4 Pin
Shuby Arora26-Mar-15 12:28
memberShuby Arora26-Mar-15 12:28 
AnswerRe: My vote +4 Pin
Gaurav Aroraa27-Mar-15 0:40
professionalGaurav Aroraa27-Mar-15 0:40 
QuestionRight idea, but Oversimplified Pin
Yvan Rodrigues25-Mar-15 15:20
professionalYvan Rodrigues25-Mar-15 15:20 
AnswerRe: Right idea, but Oversimplified Pin
Gaurav Aroraa26-Mar-15 3:09
professionalGaurav Aroraa26-Mar-15 3:09 
AnswerRe: Right idea, but Oversimplified Pin
Shuby Arora26-Mar-15 12:29
memberShuby Arora26-Mar-15 12: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181114.1 | Last Updated 25 Mar 2015
Article Copyright 2015 by Mahsa Hassankashi, Gaurav Aroraa
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid