How to Check Whether API Server is up or Down

80 篇文章 0 订阅

Introduction

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

Purpose

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

Throughout this article we will create a tiny utility to satisfy that 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 that 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);    

Do not forget to add following namespace:
  1. using System.Net.NetworkInformation;  

Call this 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 will get the following output:
Elaboration of code

Let's elaborate on the preceding code. Here PING sends an Internet Control Message Protocol (ICMP) request to the requested server host/URL and waits to receive the response back. If the value of Status is success then that means our server is up and running.

Here PingReply returns all properties, described as:

PropertyDescription
AddressGets the address of Host
BufferGets the buffer of data received, its an array of bytes
PingOptionIt handles how data transmitted
RoundTripGets the number of milliseconds to send ICMP echo request
StatusGets the status of request. If not Success , in other words Host is 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 of 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 14, 2015

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值