为什么我得到PingException?

时间:2014-05-09 09:19:36

标签: c# exception ping pingexception

这一切都是在一小时前和很多天前完成的。 我尝试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, timeout, buffer);

它跳转到catch块并抛出PingException:

  

Ping请求期间发生异常

然后在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, Int32 timeout, 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, Int32 timeout, Byte[] buffer, PingOptions options)
       InnerException:

第33行是:

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

出现此异常的原因是什么?在我的节目现在为某些人工作之前,它并没有出现。

我应该怎样或怎么处理它?<​​/ p>

2 个答案:

答案 0 :(得分:11)

您无法将完整的URL传递给Ping类的Send方法。参数string hostNameOrAddress必须是

  

一个字符串,用于标识作为ICMP回送消息目标的计算机。为此参数指定的值可以是主机名,也可以是 IP地址的字符串表示

因此,您只能传递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, timeout, buffer);

答案 1 :(得分:0)

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

“没有这样的主人知道”

我敢打赌,没有这样的主人知道。

你应该ping“www.sat24.com”而不是“http://www.sat24.com/ ...”

Ping.Send并未表示接受网址

public PingReply Send(
    string hostNameOrAddress,
    int timeout,
    byte[] buffer
)

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.

http://msdn.microsoft.com/en-us/library/ms144954(v=vs.110).aspx