Android - Detect if Wifi Requires Browser Login

See the "Handling Network Sign-On" section of the HttpUrlConnection documentation:

Some Wi-Fi networks block Internet access until the user clicks through a sign-on page. Such sign-on pages are typically presented by using HTTP redirects. You can use getURL() to test if your connection has been unexpectedly redirected. This check is not valid until after the response headers have been received, which you can trigger by calling getHeaderFields() or getInputStream().

They have a snippet of sample code there. Whether this will cover your particular WiFi AP, I can't say, but it is worth a shot.


Ping an external IP address (like google.com) to see if it responds.

    try {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("ping -c 1 " + "google.com");
        proc.waitFor();     
        int exitCode = proc.exitValue();
        if(exitCode == 0) {
            Log.d("Ping", "Ping successful!";
        } else {
            Log.d("Ping", "Ping unsuccessful.");
        }
    } 
    catch (IOException e) {}
    catch (InterruptedException e) {}

The only downside is this would also indicate that a web login is required when there is simply no internet connectivity on the WiFi access point.

@CommonsWare I believe this is a better answer than opening a UrlConnection and checking the host, since the host doesn't always change even when displaying the redirect page. For example, I tested on a Belkin router and it leaves whatever you typed in the browser as is, but still displays its own page. urlConnection.getUrl().getHost() returns what it should because of this.