有些 wifi 连接之后需要登录后才能真正上网。需要对这类 wifi 进行判断。目前发现了三种方法:
一、Handling Network Sign-On(From Google)
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;
}
网友评论