What is the best way to check for Internet connectivity using .NET?

后端 未结 27 2045
感动是毒
感动是毒 2020-11-22 07:41

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

相关标签:
27条回答
  • 2020-11-22 08:43

    A test for internet connection by pinging Google:

    new Ping().Send("www.google.com.mx").Status == IPStatus.Success
    
    0 讨论(0)
  • 2020-11-22 08:44

    The accepted answer succeeds quickly but is very slow to fail when there is no connection. So I wanted to build a robust connection check that would fail faster.

    Pinging was said to not be supported in all environments, so I started with the accepted answer and added a WebClient from here with a custom timeout. You can pick any timeout, but 3 seconds worked for me while connected via wifi. I tried adding a fast iteration (1 second), then a slow iteration (3 seconds) if the first one fails. But that made no sense since both iterations would always fail (when not connected) or always succeed (when connected).

    I'm connecting to AWS since I want to upload a file when the connection test passes.

    public static class AwsHelpers
    {
        public static bool GetCanConnectToAws()
        {
            try
            {
                using (var client = new WebClientWithShortTimeout())
                using (client.OpenRead("https://aws.amazon.com"))
                    return true;
            }
            catch
            {
                return false;
            }
        }
    }
    
    public class WebClientWithShortTimeout: WebClient
    {
        protected override WebRequest GetWebRequest(Uri uri)
        {
            var webRequest = base.GetWebRequest(uri);
            webRequest.Timeout = 5000;
            return webRequest;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:45

    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.

    0 讨论(0)
提交回复
热议问题