How to Check Whether API Server are up or down

Source: Internet
Author: User

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:

  1. public static void Serverstatusby (String URL)
  2. {    
  3. Ping pingsender = new ping ();
  4. pingreply reply = pingsender.send (URL);
  5. Console.WriteLine ("Status of Host: {0}", URL);
  6. if (reply. Status = = ipstatus.success)
  7.    {    
  8. Console.WriteLine ("IP Address: {0}", reply.    Address.tostring ());
  9. Console.WriteLine ("roundtrip time: {0}", reply.    Roundtriptime);
  10. Console.WriteLine ("Time to Live: {0}", reply.    OPTIONS.TTL);
  11. Console.WriteLine ("Don t fragment: {0}", reply.    Options.dontfragment);
  12. Console.WriteLine ("Buffer size: {0}", reply.    Buffer.length);
  13.    }    
  14. Else
  15. Console.WriteLine (reply.    Status);
  16. }
forget to add following namespace:
    1. using System.Net.NetworkInformation;

Call the method from main:

    1. Console.Write ("Enter server URL:");
    2. var url = console.readline ();
    3. console.clear ();
    4. Serverstatusby (URL);
    5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.