I'm developing Windows Phone 8 application. In this application, I have to connect to the server to get the data.
So Before connecting to the server, I want to check whether the internet connection available or not to the device. If the internet connection is available, then only I'll get the data from the server, Otherwise I'll show an error message.
Please tell me how to do this in Windows Phone 8.
This is how I did it...
Since windows phone 8 does not have a way of checking internet connectivity, you need to do it by sending a POST HTTP request. You can do that by sending it to any website you want. I chose google.com. Then, check every 10 seconds or less to refresh the state of the connection.
You can use
NetworkInformation.GetInternetConnectionProfile
to get the profile that is currently in use, from this you can work out the connectivity level. More info here GetInternetConnectionProfile msdnExample of how you might use.
NetworkInterface.GetIsNetworkAvailable()
returns the status of the NICs.Depending on the status you can ask if the connectivity is established by using:
ConnectionProfile
-Class of Windows Phone 8.1 which uses theenum NetworkConnectivityLevel
:This code should do the trick.
There can be some delay in response of web request. Therefore this method may not be fast enough for some applications. This will check the internet connection on any device. A better way is to check whether port 80, the default port for http traffic, of an always online website.
You can use
NetworkInterface.GetIsNetworkAvailable()
method. It returns true if network connection is available and false if not. And don't forget to addusing Microsoft.Phone.Net.NetworkInformation
orusing System.Net.NetworkInformation
if you are in PCL.Call this function and check whether internet connection is available or not.