美文网首页
wifi portal 检测(wifi 连接后需要登录验证的网络

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

作者: plusend | 来源:发表于2016-09-19 10:30 被阅读246次

有些 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是否有网络

相关文章

网友评论

      本文标题:wifi portal 检测(wifi 连接后需要登录验证的网络

      本文链接:https://www.haomeiwen.com/subject/icagettx.html