0

我有一个测试wifi连接是否可用的代码。

public boolean checkInternetConnection() {

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
    try {
        URL url = new URL("http://www.google.com");
        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        urlc.setRequestProperty("User-Agent", "Test");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(3000);
        urlc.setReadTimeout(4000);
        urlc.connect();
        if (urlc.getResponseCode() == HttpURLConnection.HTTP_OK) {
            Log.i(TAG,"Internet connection is OK");
            return true;
            }
        } catch (MalformedURLException mue) {
              mue.printStackTrace();
        } catch (IOException ie) {
              ie.printStackTrace();
        }
    }
    Log.i(TAG, "No internet connection.");
    return false;
}

要访问互联网,我必须通过一个接入点,这就是为什么我特别 ping 谷歌,因为如果我们也登录了我需要。我注意到在很多情况下,即使我没有登录强制门户,代码sill 到达谷歌并返回 true。我也可以上网服务器,例如 ftp。有谁知道这种行为的原因是什么?有没有其他人注意到这种行为?

谢谢,

4

1 回答 1

0

一些强制门户允许在不登录有限服务集的情况下进行访问(例如,WiFi 服务所有者的公司公司网站)。我已经看到在我多年来使用的一些 WiFi AP 中允许使用 Google 头版。这可能是你注意到的。

也许更好的方法(或附加方法)是检查网络详细状态NetworkInfo.html#getDetailedState(),它还为您提供有关强制门户检测的信息。

此外,检查您自己服务器中的特定页面可能会有所帮助(在不登录的情况下不太可能被允许),另外通过 HTTPS 而不是纯 HTTP 进行此操作可能会为您提供更多信息(获得证书不匹配错误是很常见的,因为强制门户就像一个中间人)。

于 2017-10-20T08:40:07.573 回答