How can I detect if the player has internet access in Unity (C#)?

I found a few questions/answers about this topic but they are all either outdated or not for Unity/C#.
I currently use

StartCoroutine(CheckInternetConnection(connectedToInternet =>
{
    if (connectedToInternet)
    {
        Debug.Log("Internet Available!");
        isConnected();
    }
    //if not connected to the internet
    else
    {
        Debug.Log("Internet Not Available");
        isNotConnected();
    }
}));

IEnumerator CheckInternetConnection(Action syncResult)
{
    const string echoServer = "http://google.com";

    bool result;

    using (var request = UnityWebRequest.Head(echoServer))
    {
        request.timeout = 5;
        yield return request.SendWebRequest();
        result = !(request.result == UnityWebRequest.Result.ConnectionError) && !(request.result == UnityWebRequest.Result.ProtocolError) && request.responseCode == 200;
    }

    syncResult(result);
}

This is from the answer by @Kirill Belonogov at Check for internet connectivity from Unity.

My main problem with this code is that when the user is on a public WiFi before login, this code says he is connected to the internet when in fact he is not. I tried it out at Starbucks and the library and that’s the result I get.
How can I detect that the user is connected to the internet and not when just connected to a gateway?

Read more here: Source link