为什么我会收到PingException? [英] Why i'm getting PingException?

查看:110
本文介绍了为什么我会收到PingException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个小时前和许多天前所有工作都在进行. 我尝试ping的链接是:

It was all working an hour ago and many days ago. The link i try to ping is:

链接到ping

这是form1中的代码:

This is the code in 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类中尝试执行此行时:

In the nc class when trying to do the line:

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

它正在跳转到catch块并抛出PingException:

It's jumping to the catch block and throws a PingException:

Ping请求期间发生了异常

An exception occurred during a Ping request

然后在Form1中,返回的结果是没有互联网访问但有互联网,我可以上网浏览该URL.

And then in Form1 the result it return is that there is no internet access but there is internet and I can surf to the url no problems.

这是完整的异常消息:

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

出现此异常的原因可能是什么?它从未出现过,我的程序现在已经可以用于某些yeras了.

What could be the reason that this exception show up ? it didn't show up before ever my program is working for some yeras now.

那我该怎么办?

推荐答案

您不能将完整的URL传递给

You cannot pass a full URL to the Send method of the Ping class. The parameter string hostNameOrAddress needs to be

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

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

So you can only pass in www.sat24.com or the IP of the host 82.94.176.100 (taken from the CommandLine ping www.sat24.com).

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

If you want to pass a full URL to your method you need to extract the Host from that URL to perform your Ping. For this case you can take the Uri class

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

这篇关于为什么我会收到PingException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆