C#检查Internet连接

你有没有办法告诉我,当我的C#程序运行时,是否有办法检查我的电脑中是否有互联网连接。 举一个简单的例子,如果互联网正在运行,我会输出一个消息框,说明Internet is available 。 否则我会输出一条消息说, Internet is unavailable

不使用库函数来查看网络是否可用(因为这不检查互联网连接)

 System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() 

或者没有打开网页并查看它是否返回数据

 using (WebClient client = new WebClient()) htmlCode = client.DownloadString("http://google.com"); 

因为上述两种方法都不适合我的需要。

更短的版本:

 public static bool CheckForInternetConnection() { try { using (var client = new WebClient()) using (var stream = client.OpenRead("http://www.google.com")) { return true; } } catch { return false; } } 

另一种选择是:

 Ping myPing = new Ping(); String host = "google.com"; byte[] buffer = new byte[32]; int timeout = 1000; PingOptions pingOptions = new PingOptions(); PingReply reply = myPing.Send(host, timeout, buffer, pingOptions); if (reply.Status == IPStatus.Success) { // presumably online } 

你可以在这里找到更广泛的讨论

请考虑以下代码段…

 Ping myPing = new Ping(); String host = "google.com"; byte[] buffer = new byte[32]; int timeout = 1000; PingOptions pingOptions = new PingOptions(); PingReply reply = myPing.Send(host, timeout, buffer, pingOptions); if (reply.Status == IPStatus.Success) { // presumably online } 

祝好运!

刚写了异步函数来做到这一点:

 private void myPingCompletedCallback(object sender, PingCompletedEventArgs e) { if (e.Cancelled) return; if (e.Error != null) return; if (e.Reply.Status == IPStatus.Success) { //ok connected to internet, do something... } } private void checkInternet() { Ping myPing = new Ping(); myPing.PingCompleted += new PingCompletedEventHandler(myPingCompletedCallback); try { myPing.SendAsync("google.com", 3000 /*3 secs timeout*/, new byte[32], new PingOptions(64, true)); } catch { } } 

我的NetworkMonitor类现在提供此function(基于此处的其他响应):

  public bool IsInternetAvailable { get { return IsNetworkAvailable && _CanPingGoogle(); } } private static bool _CanPingGoogle() { const int timeout = 1000; const string host = "google.com"; var ping = new Ping(); var buffer = new byte[32]; var pingOptions = new PingOptions(); try { var reply = ping.Send(host, timeout, buffer, pingOptions); return (reply != null && reply.Status == IPStatus.Success); } catch (Exception) { return false; } } 

这是我的方法;

  1. 检查网络连接是否可用,如果不可用,则我们将无法连接到其他主机。
  2. 检查我们是否可以连接到一些主要主机。 如果该站点不可用,请使用后备。

     public static bool ConnectToInternet(int timeout_per_host_millis = 1000, string[] hosts_to_ping = null) { bool network_available = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable(); if (network_available) { string[] hosts = hosts_to_ping ?? new string[] { "www.google.com", "www.facebook.com" }; Ping p = new Ping(); foreach (string host in hosts) { try { PingReply r = p.Send(host, timeout_per_host_millis); if (r.Status == IPStatus.Success) return true; } catch { } } } return false; } 

笔记:

  1. 在伸出手时不要使用太多主机,要及时衡量所有ping的成本,以防止成功的可能性减少。
  2. 如果我们将ping发送到某个我们打算稍后连接的主机(例如http请求),则返回的ping并不意味着我们连接到该特定主机。 考虑如果主机被阻止会发生什么。 例如Facebook在伊朗,中国受阻……那个ISP是否会返回ping?
  3. DNS请求不足以因为它可能被缓存
 public static bool HasConnection() { try { System.Net.IPHostEntry i = System.Net.Dns.GetHostEntry("www.google.com"); return true; } catch { return false; } }