Introduction
In this article we'll create a small utility using C # that tells us whether we have API server is up or down.
Purpose
Our purpose are to check the API server so and we can make a request for our resources. It is recommended this before making any request to the server (API server in our case), we should ensure this our server is ready and up to handle our requests.
Throughout this article we'll create a tiny utility to satisfy that purpose.
Requirements
So, what does we require? We just want to know the status of our Web API server.
Creation of Utility
Let's create a simple console app this tells us the status of our API server:
Open Visual Studio
Create A C # console application, name it "Knowserverstatus".
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);
- }
forget to add following namespace:
- using System.Net.NetworkInformation;
Call the method from main:
- Console.Write ("Enter server URL:");
- var url = console.readline ();
- console.clear ();
- Serverstatusby (URL);
- Console.ReadLine ();
Run it and we'll get the following output:
Elaboration of code
Let's elaborate on the preceding code. Here PING sends a Internet Control Message Protocol (ICMP) request to the requested server Host/url and Waits to receive the response back. If the value of the Status is success then this means our servers are up and running.
Here pingreply returns all properties, described as:
Property |
Description |
Address |
Gets the address of Host |
Buffer |
Gets the buffer of data received, its an array of bytes |
Pingoption |
IT handles how data transmitted |
Roundtrip |
Gets the number of milliseconds to send ICMP echo request |
Status |
Gets the status of request. If not Success, in other words Host was not up |
Conclusion
In this article we discussed all about the Pingreply class and using this class, we created a utility to know the status O F our server.
src:http://www.c-sharpcorner.com/uploadfile/g_arora/how-to-check-whether-api-server-is-up-or-down/
by Gaurav Kumar Arora on Mar
How to Check Whether API Server are up or down