繁体   English   中英

如何在C#中查询互联网连接状态?

[英]How to query internet connection status in C# ?

什么是最好的和快速的方式来找到用户连接到互联网?

“连接到互联网”是一个相当模糊的术语。 您可以尝试访问Internet上的特定资源,也可以采用更通用的方法查看是否可以访问默认网关。 两者都有缺点。 在第一种情况下,特定网站可能会关闭,但其他互联网访问可能没问题。 另一方面,如果您看不到默认网关,则无法访问互联网,但是如果您可以访问默认网关,则可能仍无法访问网络上所需的资源。

请记住,“访问”在这里意味着不同的东西。 您可以ping资源,但有许多资源无法应答ping(或者ping可能会被阻止)。 即,如果资源没有回答ping请求,则不一定得出结论它不可用。

您最好的选择可能是尝试在互联网上做任何您需要做的事情,然后在发生异常时处理。

尝试这个:

   1: /// <summary>
   2: /// Performs actions on the network
   3: /// </summary>
   4: public sealed class NetworkHandler
   5: {
   6:     /// <summary>
   7:     /// Private constructor to prevent compiler from generating one
   8:     /// since this class only holds static methods and properties
   9:     /// </summary>
  10:     NetworkHandler() { }
  11:  
  12:     /// <summary>
  13:     /// SafeNativeMethods Class that holds save native methods 
  14:     /// while suppressing unmanaged code security
  15:     /// </summary>
  16:     [SuppressUnmanagedCodeSecurityAttribute]
  17:     internal static class SafeNativeMethods
  18:     {
  19:         // Extern Library
  20:         // UnManaged code - be careful.
  21:         [DllImport("wininet.dll", CharSet = CharSet.Auto)]
  22:         [return: MarshalAs(UnmanagedType.Bool)]
  23:         private extern static bool 
  24:             InternetGetConnectedState(out int Description, int ReservedValue);
  25:  
  26:         /// <summary>
  27:         /// Determines if there is an active connection on this computer
  28:         /// </summary>
  29:         /// <returns></returns>
  30:         public static bool HasActiveConnection()
  31:         {
  32:             int desc;
  33:             return InternetGetConnectedState(out desc, 0);
  34:         }
  35:     }
  36: }

来源: http//blog.dotnetclr.com/archive/2007/09/24/Check-for-internet-connection---Method-2.aspx

Ping google.com?

编辑:但该怎么做?

一个快速而简单的解决方案是通过网络访问www.google.com并查看您是否可以下载任何内容:

使用套接字/ TcpClient:

try
{
  System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient("www.google.com",80);
    client.Close();
}
catch (SocketException)
{
  // Offline
}

或者您可以尝试dns查找:

try
{
  System.Net.IPHostEntry ipHostEntry= System.Net.Dns.GetHostEntry("www.google.com");
}
catch(SocketException)
{
  // Offline
}

或者你会尝试一个网络请求:

try 
{
  System.New.WebRequest.Create("http://www.google.com").GetResponse();
}
catch(WebException)
{
  // Offline
}

ping任何站点演示: http//www.dreamincode.net/code/snippet1568.htm ,你会发现这里有一些提示, 检查是否可以使用C#进行Internet连接

尝试使用Ping类并检查确切的IP地址

你最好尝试一下你需要做的任何操作,然后以适当的方式处理失败。 无论如何你需要这样做。 想一想:

  1. 您检查连接
  2. 连接稍后会消失一微秒
  3. 你试图取一些东西(或发送一些东西),以为你还在联系

尽管你做了检查,你仍然会在第3步失败,无论如何你都需要处理。 (更糟糕的是,第3步开始很好,但连接中途下降了。)那么为什么要打扰第1步呢?

AFAIK如果不尝试连接到网站,则无法测试互联网连接。 因此Rune说,最好的办法是尝试使用Google.com。 其加载速度比大多数网站都快。

DNS查询:

System.Net.Dns.GetHostAddresses( “google.com”)

比ping更快。 这应该可靠地确定用户是否连接到互联网。 在一些奇怪的场景中,您可能会得到假阴性或误报。

例如,如果google.com位于hosts文件中(机会0.000000000001%)

或者如果用户已连接但他的DNS服务器不工作/未配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM