question

MangoApps-1236 avatar image
0 Votes"
MangoApps-1236 asked ·

UWP C# How to check is internet connection is available or not?

So I want to check whether the internet connection is available or not..

So I am using "NetworkInterface.GetIsNetworkAvailable()" but it gives me the false result... its return true but in real internet connection is not available

I want a way to check internet connetion is available or not

windows-uwp
· 4
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello, did the answer below solve your problem? If not, can you provide further information?

0 Votes 0 ·

Dose @Groovykool-9087's reply help? Have you solved your issue? Please feel free to contact us if you still have problems.

0 Votes 0 ·

Hello, may I ask if you are still following this issue, if you have any other questions, you can continue to reply

0 Votes 0 ·

Hello, If you have any other questions, please feel free to contact us

0 Votes 0 ·
Ivanich avatar image
0 Votes"
Ivanich answered ·

Try NetworkInformation.GetInternetConnectionProfile() != null;


· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

pesmith avatar image
0 Votes"
pesmith answered ·

In general, the best way to check is to try the connection, and handle the failure (often by retrying with an exponential back-off). The reason is that "internet connectivity" is a surprisingly difficult thing to know: there's plenty of "weird" network configurations that will fool a simple test (and in either way).

For example, you might be behind a captive portal: every HTTP request you make return "200 OK", but in reality many of them are the portal trying to get the user to log in. Or some requests might fail, but others might pass (I've seen captive portals where simple requests were blocked by the portal, but more complex ones were let through. Or you might be in a school or business where some sites are allowed, and others are blocked. Or you might be in a country with a country-wide firewall. Or you could be in a place that allowed HTTP traffic, but not other TCP ports, or allows most TCP traffic but disallowed UDP.

Heck, where I am right now allows most internet traffic, but blocks Gopher (port 70) traffic.

Or you could have a VPN setup where the connection isn't made until it's requested by an app: until you ask to get on-line, it doesn't go on-line.

In short, just try to make the connection, and handle the error. It's what you'd have to do anyway for robustness. Your users will thank you!

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Groovykool-9087 avatar image
1 Vote"
Groovykool-9087 answered ·
     public void NetInfo()
     {
       //lastTime = DateTimeOffset.Now;
       var neti = NetworkInformation.GetInternetConnectionProfile();
       NetworkInformation.NetworkStatusChanged += NetworkStatusChanged;
    
       if (neti == null)
       {
         Comm.inetFlag = false;
         SetControlVis(false);
       }
       else
       {
         var level = neti.GetNetworkConnectivityLevel();
         UpdateNet(level); 
       }
     }
    
     private void NetworkStatusChanged(object sender)
     {
       if (timer != null)
       {
         timer.Dispose();
       }
       InetTimerSetup();
     }
    
     private void UpdateNet(NetworkConnectivityLevel level)
     {
    
       switch (level)
       {
         case NetworkConnectivityLevel.InternetAccess:
           Comm.inetFlag = true;
           SetControlVis(true);
           break;
         case NetworkConnectivityLevel.LocalAccess:
           Comm.inetFlag = false;
           SetControlVis(false);
           break;
         default:
           Comm.inetFlag = false;
           SetControlVis(false);
           break;
       }
    
     }
     private async void SetControlVis(bool state)
     {
       await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
       {
         if (state)
         {
           Ethernet.Visibility = Visibility.Visible;
           Mynet.Visibility = Visibility.Collapsed;
           Message.Visibility = Visibility.Collapsed;
           ContentGrid.IsEnabled = true;
         }
         else
         {
           Ethernet.Visibility = Visibility.Collapsed;
           Mynet.Visibility = Visibility.Visible;
           ContentGrid.IsEnabled = false;
           Message.Visibility = Visibility.Visible;
         }
       });
     }
    
    
    
     public void InetTimerSetup()
     {
       var period = (int)TimeSpan.FromSeconds(8).TotalMilliseconds;
       timer = new Timer(TimerAsync, null, period, period);
     }
    
     private async void TimerAsync(object state)
     {
       var neti = NetworkInformation.GetInternetConnectionProfile();
       if (neti == null)
       {
         Comm.inetFlag = false;
         SetControlVis(false);
       }
       else
       {
         var level = neti.GetNetworkConnectivityLevel();
         UpdateNet(level);  
       }
       await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
           () =>
           {
             if (Comm.inetFlag && Comm.pageType == typeof(NetDown))
             {
               NavigationService.Navigate(typeof(ContentGridPage));
             }
    
             if (!Comm.inetFlag && (Comm.pageType == typeof(ContentGridPage)))
             {
               NavigationService.Navigate(typeof(NetDown));
             }
    
           });
    
    
     }
· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.