0

I know there is solution something like this:

public static bool HasConnection()
 {
     try
     {
         using (var client = new WebClient())
         using (var stream = new WebClient().OpenRead("http://www.google.com"))
         {
             return true;
         }
     }
     catch
     {
         return false;
     }
 }

But my question: is using google service a good practice for access checking?

I mean if an app has a lot of downloads this solution provide big amount of traffic for google. I think in this case google can block you app, can't it?

On the other hand I can use an own server, but I haven't a powerful enough one.

So what other solutions are exist?

  • Hehe Google has so many accesses each day that I doubt a lot that your app will ever have enough instances running that they would even notice ;) – derHugo Jan 03 '22 at 13:47
  • 1
    Probably a bit more efficient though since you are not interested in downloading anything but rather only check availability might be a simple [`Ping`](https://learn.microsoft.com/dotnet/api/system.net.networkinformation.ping?view=netframework-4.7) against e.g. `8.8.8.8` which is Google's public DNS server – derHugo Jan 03 '22 at 13:50
  • @derHugo yeah, you're right, I think google even won't notice this traffic, but what do you think about availability of google service? Does it available anywhere? – Alexander Tukanov Jan 03 '22 at 14:08
  • Define "anywhere" ;) Probably not e.g. in [North Korea](https://en.wikipedia.org/wiki/Censorship_in_North_Korea) or [China](https://www.comparitech.com/privacy-security-tools/blockedinchina/google/) (though the ping might even work) ... but in general I would claim if Google isn't reachable then also the rest of the internet is at least restricted so I'd say it's a good general indicator for internet accessibility ;) – derHugo Jan 03 '22 at 14:25
  • @derHugo Ok, thank you! I got that I can ping google and some China service – Alexander Tukanov Jan 03 '22 at 14:35
  • Well these are only two examples .. I don't know about any spontaneously but there might be other countries/regions with restrictive governments ;) – derHugo Jan 03 '22 at 14:36
  • 1
    Oh btw there is also a Unity [`Ping`](https://docs.unity3d.com/ScriptReference/Ping.html) which also works for Android ... not sure if afore mentioned the c# `Ping` is available on Android – derHugo Jan 03 '22 at 16:35

0 Answers0