🌱PING 地址/主机名/域名
/// <summary>
/// PING
/// </summary>
/// <param name="ip">ip</param>
/// <returns></returns>
public static bool PingIp(string ip)
{
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true;
string data = "Test Data!";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 2000; // Timeout
System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
{
// AddToConvo(ip + reply.Status);
return true;
}
else
{
// AddToConvo(ip + reply.Status);
return false;
}
}
👀调用方法
List<string> list = new List<string>();
list.Add("192.168.1.1");
list.Add("192.168.3.1");
list.Add("192.168.4.1");
foreach (string s in list)
{
Console.WriteLine(s+" "+ ccPing.PingIp(s));
//if(!xxx)
}
Thread.Sleep(10000);
隔10秒自动调用1次
📫检查URL
public async Task<bool> IsServerRespondingAsync(string url, TimeSpan timeout)
{
try
{
using (var cancellationTokenSource = new System.Threading.CancellationTokenSource())
{
cancellationTokenSource.CancelAfter(timeout);
var response = await _httpClient.GetAsync(url, cancellationTokenSource.Token);
return response.IsSuccessStatusCode;
}
}
catch (TaskCanceledException)
{
// 请求超时
return false;
}
catch (Exception)
{
// 发生其他错误
return false;
}
}
⚡异步调用
await checker.IsServerRespondingAsync(url, TimeSpan.FromSeconds(2));
如果False可以调用报警代码文章来源:https://www.toymoban.com/news/detail-860773.html
END文章来源地址https://www.toymoban.com/news/detail-860773.html
到了这里,关于.NET 检测地址/主机/域名是否正常的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!