为什么我会收到PingException异常?

12
一个小时之前和很多天之前都可以运行。我尝试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);

为什么会出现这个异常?在过去几年中,我的程序一直在工作,从未遇到过这种情况。

我应该如何处理它?


你是否在使用代理? - Yahya
不,我没有落后。 - user3596190
第33行没有问题,问题在于ping请求。 - Arjun Chaudhary
在命令行中尝试使用 ping "http://www.sat24.com/image.ashx?country=afis&type=slide&time=&ir=true&index=1&sat="。它能正常工作吗? - Thomas Weller
2个回答

16

在 Ping 类的 Send 方法中,您不能传递完整的 URL。参数string hostNameOrAddress需要是一个字符串,用于标识作为 ICMP 回显消息目标的计算机。此参数的值可以是主机名IP 地址的字符串表示形式

因此,您只能传入www.sat24.com或主机的 IP:82.94.176.100(从 CommandLine ping www.sat24.com 获取)。

如果您想要将完整的 URL 传递给方法,则需要从该 URL 提取主机以执行 ping。对于此情况,您可以使用 Uri 类。

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

谢谢。我的IP地址有一个错别字,是逗号而不是句点。很难发现! - Valamas

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

"找不到此主机"

我打赌是没有这样的主机。

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

Ping.Send并不接受URL。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接