這一切都在一小時前和許多天前工作。 我嘗試ping鏈接是:爲什麼我得到PingException?
這是在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);
可能是什麼,這種異常出現的原因是什麼?它在我的計劃現在爲某些yeras工作之前沒有出現。
我該怎麼處理它?
你是否在代理? – Yahya
Yaha no。我不落後。 – user3596190
第33行不是問題,在ping請求中。 –