Android - Detect if Wifi Requires Browser Login

后端 未结 5 2138
时光说笑
时光说笑 2020-12-29 07:25

My university has an open wifi access point, however it requires you to enter your e-mail before it allows you to use the web. My problem is that the Wifi is stupid in that

5条回答
  •  心在旅途
    2020-12-29 08:09

    I used the following code using google's 204 endpoint.

     private boolean networkAvailable() {
    
            ConnectivityManager mManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            if(mManager != null) {
                NetworkInfo activeNetwork = mManager.getActiveNetworkInfo();
                if(activeNetwork== null || !activeNetwork.isConnectedOrConnecting()){
                    return false;
                }
            }
    
            OkHttpClient client = new OkHttpClient();
    
            Request request = new Request.Builder()
                    .url("http://clients1.google.com/generate_204")
                    .build();
            try {
                Response response = client.newCall(request).execute();
                if(response.code() != 204)
                    return  false; // meaning it either responded with a captive html page or did a redirection to captive portal.
                return true;
            } catch (IOException e) {
                return true;
            }
        }
    

    Many applications including Google Chrome use http://clients1.google.com/generate_204 to verify that the the connection is not locked under captive portal.

提交回复
热议问题