2010-09-27 58 views

回答

4

試試這個:

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

2

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 
} 

或者你想嘗試一個Web請求:

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

它需要2-3秒左右的時間,我需要更快的時間少於0.25秒。 – Ata 2010-09-27 08:36:35

+0

我會在應用程序啓動時在後臺線程上執行檢查。當我需要知道時,我會知道我是否在線。 – 2010-09-27 08:52:42

+0

另外,您應該知道您做的任何檢查可能會失敗,並且您可能仍然在線。因此,即使您看起來處於離線狀態,您仍應允許用戶嘗試此過程。 – 2010-09-27 08:53:50

0

找到一些竅門嘗試使用Ping class和檢查確切IP地址

4

「連接到Internet」是一個非常模糊的術語。您可以嘗試訪問互聯網上的特定資源,或者尋求更一般的方法,並查看您是否可以訪問默認網關。兩者都有缺點。在第一種情況下,特定的網站可能會關閉,但其他互聯網訪問可能沒問題。另一方面,如果您看不到默認網關,則無法訪問Internet,但如果您可以訪問默認網關,則可能仍無法訪問網絡上所需的資源。

請記住,「訪問」在這裏意味着不同的事情。你可以ping資源,但有很多資源不能回答ping(或ping可能被阻塞)。即如果資源沒有應答ping請求,您不一定會得出它不可用的結論。

最好的辦法是嘗試做任何你需要在互聯網上做的事情,然後處理髮生的異常。

+0

+1「你最好的辦法可能是嘗試做任何你需要在互聯網上做的事情,然後在事件發生時處理異常情況。你在這裏教我:) – 2010-09-27 08:59:44

0

如果您嘗試所需的任何操作,然後以適當的方式處理故障,您將會好得多。無論如何你都需要這樣做。想想看:

  1. 您檢查連接
  2. 連接中斷了微秒後
  3. 你嘗試獲取的東西(或送東西),以爲你還在連接

儘管你檢查了,你仍然會在步驟3失敗,無論如何你都需要處理。 (更糟的是,第3步開始正常,但連接中途中斷。)那麼爲什麼要打擾第1步呢?

0

AFAIK如果不嘗試連接到網站,則無法測試互聯網連接。所以最好的選擇就是像Rune所說的那樣嘗試Google.com。其加載速度比大多數他的網站。

0

DNS查詢:

System.Net.Dns.GetHostAddresses( 「google.com」)比平

更快。這應該可靠地確定用戶是否連接到互聯網。在一些奇怪的場景中,您可能會得到錯誤的否定或誤報。

如果google.com在例如hosts文件(機率0.000000000001%)

,或者如果用戶已連接,但他的DNS服務器不工作/未配置。