Reputation: 133
I'm having a problem with this verification of the internet connection. See I have this verification in my code:
if (CrossConnectivity.Current.IsConnected)
{
...
}
It works, but I think there is a situation that this do not cover, I have a shopping right next door to my office, I enter the shopping and the device just connects with the shopping WI-FI, but it's not passing data, because it need's to login in the session to get free WI-FI from the shopping.
So my question in resume is, is there a way, not only to verify if the device is conected to the internet, but if it is passing data through the connection?
Upvotes: 0
Views: 172
Reputation: 133
Thanks everyone, I see all answers work in my situation, but I used this method from https://jamesmontemagno.github.io/ConnectivityPlugin/PingaHost.html, you could open it or just see it down here:
var verificaPassagemDados = await CrossConnectivity.Current.IsRemoteReachable("google.com");
Thanks @sme
Upvotes: 0
Reputation: 3216
You could use System.Net.WebClient
and test open/read an url. Also another way could be to ping a resource that is theoritically never offline. i.e. google.
Something like:
if (CrossConnectivity.Current.IsConnected)
{
try {
Ping ping = new Ping();
String host = "google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = ping.Send(host, timeout, buffer, pingOptions);
if (reply.Status == IPStatus.Success){
// Your code here...
}
}
catch (Exception) {
return false;
}
}
Hope it helps.
Upvotes: 1
Reputation: 17452
I didn't try this solution but it might help you.
if (CrossConnectivity.Current.IsConnected)
{
if (ConnectGoogle())
{
return true;
}
else
{
//
}
}
ConnectGoogle
method
public bool ConnectGoogle()
{
try
{
HttpURLConnection urlc = (HttpURLConnection)(new URL("http://www.google.com").OpenConnection());
urlc.SetRequestProperty("User-Agent", "Test");
urlc.SetRequestProperty("Connection", "close");
urlc.ConnectTimeout = 10000;
urlc.Connect();
return (urlc.ResponseCode == HttpStatus.Accepted);
}
catch (Exception ex)
{
//Log(ex.Message);
return false;
}
}
Upvotes: 1