PingToolController.cs 2.35 KB
using System;using System.Collections.Generic;using System.Net.NetworkInformation;
using System.Text;using Hh.Mes.Common.Json;using Hh.Mes.Common.log;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service;using Hh.Mes.Service.SystemAuth;
using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Http.Headers;
using Microsoft.AspNetCore.Mvc;// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebMvc{    [Route("api/[controller]/[action]")]    [ApiController]    public  class PingToolController : BaseController    {        public PingToolController(IAuth authUtil,IHttpContextAccessor accessor):base(authUtil)        {            this.context = accessor.HttpContext;        }

        /// <summary>        /// Ping数据 http://127.0.0.1:6001/api/PingTool/PingIP        /// </summary>        /// <returns></returns>        [HttpGet]        [ActionName("PingIP")]        public string PingIP(string ip)        {            
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                string host = ip;
                Ping p1 = new Ping();
                PingReply reply = null;
                try
                {
                    if (Program.PingTimeout > 0)
                    {
                        reply = p1.Send(host, Program.PingTimeout);
                    }
                    else
                    {
                        reply = p1.Send(host);
                    }
                }
                catch (Exception ex)
                {
                    response.Result = "异常";
                    return Serialize(response);
                }
                
                string result = "未知";
                if (reply.Status == IPStatus.Success)
                {
                    result = "成功";
                }
                else if (reply.Status == IPStatus.TimedOut)
                {
                    result = "超时";
                }
                else
                {
                    result = "失败";
                }
                response.Result = result;
                return Serialize(response);
            });
        }    }}