i know its a big discussion how to check if there is a internet connection available for the device.
I tried Ping, WebClient and HTTPClient. I also use Xamarin Essentials and the Connectivity Plugin.
All this things are working, just make a request to google or the server of your choice and you will get back the answer or not. You can also set a timeout for 2 seconds and so on.
But now i came to the situation where i'm connected to the WIFI BUT the WIFI itself has no active internet connection.
So all the things i wrote about was not working anymore. The problem is that the timeout will be ignored somehow. Maybe a bug in .net? I don't know.
Now i found one last thing:
try
{
var request = (HttpWebRequest)WebRequest.Create("https://www.google.com");
request.KeepAlive = false;
request.Timeout = 2000;
using (var response = (HttpWebResponse)await request.GetResponseAsync())
{
if (response.StatusCode == HttpStatusCode.OK)
{
//Connection to internet available
return true;
}
else
{
//Connection to internet not available
return false;
}
}
}
catch (WebException webEx)
{
if (webEx.Status == WebExceptionStatus.Timeout)
{
return false;
}
}
catch (Exception e)
{
return false;
}
This was the only solution where i got the WebException when the 2 seconds timeout was reached. In all other solutions i stuck more than 1 minute till the timeout was reached. Also when i set it to 500ms or something.
Did anybody know the reason why the given timeout is not reached for example with other methodes about?