.net core6 检测ip网址是否可访问。不需要端口号。
Posted 棉晗榜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.net core6 检测ip网址是否可访问。不需要端口号。相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleAppNetworkTestAndToEmail
/// <summary>
/// 检测URL是否连通
/// </summary>
/// 创建时间:2022-12-16 13:55:51。
internal class PingURLHelpter
/// <summary>
/// 检测ip、网址是否可访问。不需要端口号。
/// ip格式:192.168.1.226;
/// 网址格式:www.baidu.com
/// </summary>
/// <param name="ipAddress">ip地址</param>
/// <param name="timeout">预设的超时时间,单位(毫秒)</param>
/// <returns></returns>
/// 创建时间:2022-12-16 14:01:02。
internal static PingResult TcpIpPing(string host, int timeout = 3000)
try
var pingHandle = new System.Net.NetworkInformation.Ping();
var result = pingHandle.Send(host, timeout);
if (result.Status == System.Net.NetworkInformation.IPStatus.Success)
string msg = $"原host,解析地址result.Address连接成功,返回字节result.Buffer.Length,响应毫秒数result.RoundtripTime";
LogHelpter.AddLog(msg);
return new PingResult() HttpStatusCode = 200, Tested_time_consuming = result.RoundtripTime ;
else
string msg = $"原host,解析地址result.Address访问失败 ,响应毫秒数result.RoundtripTime";
LogHelpter.AddLog(msg);
return new PingResult() HttpStatusCode = 500, Tested_time_consuming = 0 ;
catch (Exception ex)
LogHelpter.AddLog("检测异常2," + ex.Message);
return new PingResult() HttpStatusCode = 500, Tested_time_consuming = 0 ;
/// <summary>
/// 返回值
/// </summary>
internal struct PingResult
/// <summary>
/// 返回的响应状态码,返回200表示连接成功。
/// </summary>
public int HttpStatusCode get; set;
/// <summary>
/// 耗时,单位毫秒
/// </summary>
public long Tested_time_consuming get; set;
参考了博文
http://www.bilibili996.com/Course?id=5190249000105
以上是关于.net core6 检测ip网址是否可访问。不需要端口号。的主要内容,如果未能解决你的问题,请参考以下文章