【问题标题】:Why i'm getting PingException?为什么我会收到 PingException?
【发布时间】:2023-04-05 06:33:01
【问题描述】:

一小时前和很多天前都还在工作。 我尝试 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 中返回的结果是没有互联网访问但有互联网,我可以冲浪到 url 没有问题。

这是完整的异常消息:

  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);

出现此异常的原因可能是什么?它在我的程序现在运行了一些年之前没有出现。

我应该怎么处理?

【问题讨论】:

  • 你在代理后面吗?
  • 雅哈没有。我没有落后。
  • 第 33 行没有问题 问题在 ping 请求中。
  • 在命令行尝试ping "http://www.sat24.com/image.ashx?country=afis&type=slide&time=&ir=true&index=1&sat="。有用吗?

标签: c# exception ping pingexception


【解决方案1】:

您不能将完整的 URL 传递给 Ping 类的 Send 方法。参数string hostNameOrAddress需要是

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

所以你只能传入www.sat24.com或主机的IP82.94.176.100(取自命令行ping www.sat24.com)。

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

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

【讨论】:

  • 谢谢。我的IP地址错字。逗号而不是句号。很难发现!
【解决方案2】:
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

【讨论】:

    猜你喜欢
    • 2016-08-21
    • 2014-07-04
    • 2020-06-06
    • 2018-12-26
    • 2021-03-05
    • 2013-07-13
    • 2019-05-28
    • 2018-07-01
    相关资源
    最近更新 更多