【发布时间】:2023-04-05 06:33:01
【问题描述】:
一小时前和很多天前都还在工作。 我尝试 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