What is the best way to check for Internet connect

2018-12-31 17:25发布

What is the fastest and most efficient way to check for Internet connectivity in .NET?

23条回答
墨雨无痕
2楼-- · 2018-12-31 18:13

A test for internet connection by pinging Google:

new Ping().Send("www.google.com.mx").Status == IPStatus.Success
查看更多
看淡一切
3楼-- · 2018-12-31 18:13

I have seen all the options listed above and the only viable option to check wither the internet is available or not is the "Ping" option. Importing [DllImport("Wininet.dll")] and System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces() Or any other variation of the NetworkInterface class does not work well in detecting the availability of the network.These Methods only check if the network cable is plugged in or not.

The "Ping option"

if(Connection is available) returns true

if(Connection is not available and the network cable is plugged in) returns false

if(Network cable is not plugged in) Throws an exception

The NetworkInterface

if(Internet Is available)Returns True

if(Internet is not Available and Network Cable is Plugged in ) Returns True

if(Network Cable is Not Plugged in )returns false

The [DllImport("Wininet.dll")]

if(Internet Is available)Returns True

if(Internet is not Available and Network Cable is Plugged in ) Returns True

if(Network Cable is Not Plugged in )returns false

So in case of [DllImport("Wininet.dll")] and NetworkInterface There is no way of knowing if internet connection is available.

查看更多
不流泪的眼
4楼-- · 2018-12-31 18:13

If you want to notify the user/take action whenever a network/connection change occur.
Use NLM API:

查看更多
明月照影归
5楼-- · 2018-12-31 18:13

I have three tests for an Internet connection.

  • Reference System.Net and System.Net.Sockets
  • Add the following test functions:

Test 1

public bool IsOnlineTest1()
{
    try
    {
        IPHostEntry dummy = Dns.GetHostEntry("https://www.google.com");
        return true;
    }
    catch (SocketException ex)
    {
        return false;
    }
}

Test 2

public bool IsOnlineTest2()
{
    try
    {
        IPHostEntry dummy = Dns.GetHostEntry("https://www.google.com");
        return true;
    }
    catch (SocketException ex)
    {
        return false;
    }
}

Test 3

public bool IsOnlineTest3()
{
    System.Net.WebRequest req = System.Net.WebRequest.Create("https://www.google.com");
    System.Net.WebResponse resp = default(System.Net.WebResponse);
    try
    {
        resp = req.GetResponse();
        resp.Close();
        req = null;
        return true;
    }
    catch (Exception ex)
    {
        req = null;
        return false;
    }
}

Performing the tests

If you make a Dictionary of String and Boolean called CheckList, you can add the results of each test to CheckList.

Now, recurse through each KeyValuePair using a for...each loop.

If CheckList contains a Value of true, then you know there is an Internet connection.

查看更多
无与为乐者.
6楼-- · 2018-12-31 18:14

Something like this should work.

System.Net.WebClient

public static bool CheckForInternetConnection()
{
    try
    {
        using (var client = new WebClient())
        using (client.OpenRead("http://clients3.google.com/generate_204"))
        {
            return true;
        }
    }
    catch
    {
        return false;
    }
}
查看更多
长期被迫恋爱
7楼-- · 2018-12-31 18:15
private bool ping()
{
    System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
    System.Net.NetworkInformation.PingReply reply = pingSender.Send(address);
    if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
    {                
        return true;
    }
    else
    {                
        return false;
    }
}
查看更多
登录 后发表回答