wifi portal 检测(wifi 连接后需要登录验证的网络检测)

有些 wifi 连接之后需要登录后才能真正上网。需要对这类 wifi 进行判断。目前发现了三种方法:

一、Handling Network Sign-On(From Google)

参考 HttpURLConnection

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(). For example, to check that a response was not redirected to an unexpected host:

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();   
try {     
    InputStream in = new BufferedInputStream(urlConnection.getInputStream());     
    if (!url.getHost().equals(urlConnection.getURL().getHost())) {       // we were redirected! Kick the user out to the browser to sign on?     
    }
    ...
} finally {
    urlConnection.disconnect();
}

二、http://clients3.google.com/generate_204

参考 wifi portal 检测(wifi连接后需要登陆验证的网络判断)

三、ping -c 1 114.114.114.114

public boolean isNetworkOnline() {
    Runtime runtime = Runtime.getRuntime();
    try {
        Process ipProcess = runtime.exec("ping -c 1 114.114.114.114");
        int exitValue = ipProcess.waitFor();
        return (exitValue == 0);
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
    return false;
}

参考 android判断连接的wifi是否有网络

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容