- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我需要可靠地检测设备是否具有完整的互联网访问权限,即用户不限于强制门户(也称为围墙花园),即有限的子网,它强制用户在表单上提交他们的凭据以获得完全访问权限。
我的应用程序正在自动执行身份验证过程,因此在开始登录 Activity 之前了解无法完全访问互联网非常重要。
问题不是关于如何检查网络接口(interface)是否已启动并处于连接状态。这是为了确保设备具有不受限制的互联网访问权限,而不是沙盒内网段。
到目前为止我尝试过的所有方法都失败了,因为连接到任何知名主机都不会引发异常,而是返回有效的 HTTP 200
响应代码,因为所有请求都被路由到登录页面。
这是我尝试过的所有方法,但由于上述原因,它们都返回 true
而不是 false
:
1:
InetAddress.getByName(host).isReachable(TIMEOUT_IN_MILLISECONDS);
isConnected = true; <exception not thrown>
2:
Socket socket = new Socket();
SocketAddress sockaddr = new InetSocketAddress(InetAddress.getByName(host), 80);
socket.connect(sockaddr, pingTimeout);
isConnected = socket.isConnected();
3:
URL url = new URL(hostUrl));
URLConnection urlConn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.setAllowUserInteraction(false);
httpConn.setRequestMethod("GET");
httpConn.connect();
responseCode = httpConn.getResponseCode();
isConnected = responseCode == HttpURLConnection.HTTP_OK;
那么,如何确保我连接到实际主机而不是登录重定向页面?显然,我可以从我使用的“ping”主机检查实际响应正文,但它看起来不是一个合适的解决方案。
最佳答案
作为引用,这里是来自 Android 4.0.1 AOSP 代码库的“官方”方法: WifiWatchdogStateMachine.isWalledGardenConnection() .我将下面的代码包括在内,以防万一将来链接断开。
private static final String mWalledGardenUrl = "http://clients3.google.com/generate_204";
private static final int WALLED_GARDEN_SOCKET_TIMEOUT_MS = 10000;
private boolean isWalledGardenConnection() {
HttpURLConnection urlConnection = null;
try {
URL url = new URL(mWalledGardenUrl); // "http://clients3.google.com/generate_204"
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setInstanceFollowRedirects(false);
urlConnection.setConnectTimeout(WALLED_GARDEN_SOCKET_TIMEOUT_MS);
urlConnection.setReadTimeout(WALLED_GARDEN_SOCKET_TIMEOUT_MS);
urlConnection.setUseCaches(false);
urlConnection.getInputStream();
// We got a valid response, but not from the real google
return urlConnection.getResponseCode() != 204;
} catch (IOException e) {
if (DBG) {
log("Walled garden check - probably not a portal: exception "
+ e);
}
return false;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
这种方法依赖于一个特定的 URL,mWalledGardenUrl = "http://clients3.google.com/generate_204"
总是返回一个 204
响应代码。即使 DNS 受到干扰,这也会起作用,因为在这种情况下,将返回 200
代码而不是预期的 204
。我看到一些强制门户网站欺骗了对这个特定 URL 的请求,以防止在 Android 设备上显示 Internet 无法访问 消息。
Google 有这个主题的变体:获取 http://www.google.com/blank.html
将返回一个具有零长度响应的 200
代码 body 。所以如果你得到一个非空的 body ,这将是另一种判断你在围墙花园后面的方法。
Apple 有自己的 URL 来检测强制门户:当网络启动时,IOS 和 MacOS 设备将连接到像 http://www.apple.com/library/test/success.html 这样的 URL , http://attwifi.apple.com/library/test/success.html , 或 http://captive.apple.com/hotspot-detect.html它必须返回 200
的 HTTP 状态代码和包含 Success
的正文。
NOTE:This approach will not work in areas with regionally restricted Internet access such as China where the whole country is a walled garden, and where some Google/Apple services might be blocked. Some of these might not be blocked:
http://www.google.cn/generate_204
,http://g.cn/generate_204
,http://gstatic.com/generate_204
orhttp://connectivitycheck.gstatic.com/generate_204
— yet these all belong to google so not guaranteed to work.
关于android - 如何检查不受限制的 Internet 访问? (强制门户检测),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13958614/
根据此 Microsoft 指南: https://learn.microsoft.com/en-us/azure/app-service/webjobs-create右键单击 Web 作业时,应该会
我正在尝试在新的 Azure 门户中将网站的开发人员组添加到读者角色,并将用户分配到该组。 当我将特定用户添加到网站的读者角色时,用户可以正常看到所有内容,但是当我删除该用户并将其添加到开发人员组时,
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我无法验证 Azure Portal在我的家庭桌面(Windows 10 家庭版)上。它只是挂着以下图标,直到我最终被重定向到 error timeout page . 有趣的是,我仍然可以登录 Of
我知道我的问题并不具体,也没有明确的答案,但我需要帮助来弄清楚从哪里开始寻找。 因此,我们在 Azure 门户上工作,并有一个 Web 作业从队列中获取消息并处理它们;由于一些奇怪的原因,当我使用 S
昨天共享我的 Azure 门户仪表板后,我想我不小心删除了我的默认仪表板,因为它与共享仪表板同名。现在,当我登录门户时,我收到以下消息: Dashboard not found We could no
过去几天我收到一个非常奇怪的错误。当我尝试在 Azure 门户中打开任何 SQL Azure 数据库时,我只是在通知中心收到一条错误消息: Refresh the browser to try aga
我在 BizTalk 方面有相当丰富的经验,但对 ESB 工具包还很陌生。我们实际上并不需要这样的 ESB 解决方案,但我想使用 ESB Portal 来显示错误、修改消息并重新提交。 据我所知,我已
昨天共享我的 Azure 门户仪表板后,我想我不小心删除了我的默认仪表板,因为它与共享仪表板同名。现在,当我登录门户时,我收到以下消息: Dashboard not found We could no
过去几天我收到一个非常奇怪的错误。当我尝试在 Azure 门户中打开任何 SQL Azure 数据库时,我只是在通知中心收到一条错误消息: Refresh the browser to try aga
错误 500:java.util.MissingResourceException:找不到 bundle com.ibm.abc.nl.abcdf、 key en_US 的资源。 我收到此异常。我在
我正在尝试编写一个程序,以编程方式将用户登录到 ArcGIS 门户。 这是我的场景: 用户登录到应用程序 A,用户单击指向 ArcGIS 门户的链接 - 我不希望他们在单击该链接时必须登录门户,因为他
React portals让您将 React 子元素渲染到不同的 DOM 元素中,在页面上完全独立的某个位置。 我想做相反的事情:我想在页面的其他地方渲染一次 DOM 元素,然后根据我的 React
我一直在使用 Azure 搜索通过 Azure 门户远程创建的索引 (Umbraco) 中进行搜索。 今天早上,我收到错误消息,告诉我在索引中允许门户来源。 我不明白为什么我昨天能够搜索,而今天它告诉
我曾使用 Websphere Portal 6.0、6.1 并使用 JSR 168、JSR 286 Portlets 开发了门户应用程序。 现在我正在转移到我将使用 Liferay 门户服务器和 JS
如何使用 portal.properties 配置或覆盖 Liferay 门户属性? 最佳答案 Liferay 属性是一种强大的工具,它允许我们配置 Portal 行为而无需编写 hooks-、ext
我已经安装了 FIM Portal 并且安装成功。但是,我无法在 FI 上查看导航栏和管理控件 最佳答案 在集合 --> 搜索管理员 --> 将您的用户 ID 添加到该集合。 关于sharepoint
我的 Web 应用程序部署在 Azure 门户中。我想从我的解决方案中删除整个 web.config 文件,并想在 Azure 中配置它。是否可以做到这一点,如果可能的话,我需要在哪里提供azure中
我认为这是不可能的,但我想知道是否有任何网址可以将我重定向到标准逻辑应用程序(单租户)中的特定工作流程。 我为什么要问这个问题,因为在消费逻辑应用程序中,如果我向某人提供 URL,他们会直接进入概述页
我是一名优秀的程序员,十分优秀!