c# – 为什么我得到PingException?

c# – 为什么我得到PingException?,第1张

概述这一切都是在一小时前和很多天前完成的. 我尝试ping的链接是: Link to ping 这是form1中的代码: nc = new NetworkConnection();bool bval = nc.PingConnection(satellite_address);if (bval){ label19.Visible = true; label19.Text = " 这一切都是在一小时前和很多天前完成的.
我尝试Ping的链接是:

Link to ping

这是form1中的代码:

nc = new NetworkConnection();bool bval = nc.PingConnection(satellite_address);if (bval){    label19.Visible = true;    label19.Text = "Internet Access";}else{    label19.Visible = true;    label19.Text = "No Internet Access";}

当它试图执行此行时:

bool bval = nc.PingConnection(satellite_address);

它将进入nc类:

using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Net;using System.Net.networkinformation;using System.IO;using System.windows.Forms;namespace mws{    class NetworkConnection    {        public NetworkConnection()        {            }        public bool PingConnection(string url)        {            bool Result = false;            using (Ping pp = new Ping())            {                byte[] buffer = EnCoding.ASCII.GetBytes("samplestring");                int timeout = 120;                try                {                    PingReply reply = pp.Send(url,timeout,buffer);                    if (reply.Status == IPStatus.Success)                        Result = true;                }                catch (Exception)                {                    Result = false;                }            }            return Result;        }    }}

在尝试执行该行时在nc类中:

PingReply reply = pp.Send(url,buffer);

它跳转到catch块并抛出PingException:

An exception occurred during a Ping request

然后在Form1中返回的结果是没有互联网访问,但有互联网,我可以冲浪到网址没有问题.

这是完整的异常消息:

System.Net.networkinformation.PingException was caught  HResult=-2146233079  Message=An exception occurred during a Ping request.  Source=System  StackTrace:       at System.Net.networkinformation.Ping.Send(String hostnameOrAddress,Int32 timeout,Byte[] buffer,PingOptions options)       at System.Net.networkinformation.Ping.Send(String hostnameOrAddress,Byte[] buffer)       at mws.NetworkConnection.PingConnection(String url) in d:\C-Sharp\Download file\Downloading-file-Project-Version-012\Downloading file\NetworkConnection.cs:line 33  InnerException: System.Net.sockets.socketException       HResult=-2147467259       Message=No such host is kNown       Source=System       ErrorCode=11001       NativeErrorCode=11001       StackTrace:            at System.Net.Dns.GetAddrInfo(String name)            at System.Net.Dns.InternalGetHostByname(String hostname,Boolean includeIPv6)            at System.Net.Dns.GetHostAddresses(String hostnameOrAddress)            at System.Net.networkinformation.Ping.Send(String hostnameOrAddress,PingOptions options)       InnerException:

第33行是:

PingReply reply = pp.Send(url,buffer);

这个例外出现的原因是什么?在我的节目现在为一些人工作之前它没有出现.

我应该怎样或怎么处理它?

解决方法 您无法将完整URL传递给Ping类的 Send方法.参数字符串hostnameOrAddress需要

A String that IDentifIEs the computer that is the destination for the ICMP echo message. The value specifIEd for this parameter can be a host name or a string representation of an IP address.

因此,您只能传入www.sat24.com或主机82.94.176.100的IP(取自Commandline Ping www.sat24.com).

如果要将完整URL传递给方法,则需要从该URL中提取主机以执行Ping.对于这种情况,你可以参加Uri课程

Uri uri = new Uri(url);PingReply reply = pp.Send(uri.Host,buffer);
总结

以上是内存溢出为你收集整理的c# – 为什么我得到PingException?全部内容,希望文章能够帮你解决c# – 为什么我得到PingException?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1244958.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存