简体   繁体   English

如何查看您的网络应用程序是否无法连接互联网

[英]How to find out if the internet connection is not available from your web app

How to find out if the internet connection is not available from your j2ee web app, since some portions of our UI will not work properly if an internet connection is not available. 如何确定您的j2ee网络应用程序是否无法使用互联网连接,因为如果没有互联网连接,我们UI的某些部分将无法正常工作。 So instead of failing with a bad exception, I would like to fail gracefully. 因此,我希望优雅地失败,而不是失败。

The only universal way to test if "the internet" is working is to attempt to retrieve the resource you want. 测试“互联网”是否正常工作的唯一通用方法是尝试检索所需的资源。 Anything else potentially can give you misleading or unhelpful results under certain circumstances. 在某些情况下,任何其他可能会给你带来误导或无益的结果。

For instance, if you use some site that is "nearly always available" as a proxy for the internet working ... it might be down or inaccessible when the site you really want to talk to is up and accessible. 例如,如果您使用某个“几乎总是可用”的网站作为互联网工作的代理......当您真正想要与之交谈的网站启用并可访问时,它可能会停止或无法访问。 Or vice versa. 或相反亦然。 In the former case, your application might conclude that "the internet" is unavailable and not even try to access the site you want to access. 在前一种情况下,您的应用程序可能会断定“互联网”不可用,甚至没有尝试访问您要访问的站点。 In the latter case, your application's diagnosis that "the internet" is available is immaterial to accessing the site it really needs to talk to. 在后一种情况下,您的应用程序诊断“互联网”可用对于访问它真正需要与之交谈的网站并不重要。

Typical advanced desktop apps that haven't been developed in the 19th century and that need an "Internet connection" have an Network status... menu which shows something like: 典型的高级桌面应用程序尚未在19世纪开发并且需要“Internet连接”,其网络状态...菜单显示如下:

Manx Telecom   80.65.254.253
Cambridge, UK  131.111.8.46
US West Coast  169.232.56.224
Yahoo          etc.
etc.           etc.

Find a list of IP that have been known to respond to pings for years, put 10 of them in your software. 查找已知多年来响应ping的IP列表,将其中的10个放入您的软件中。

If, say, at least 3 of them answer to a ping you know with a very high level of certainty that your app has full Internet connectivity. 例如,如果他们中至少有3个人对您的应用程序具有完全互联网连接的确定性有所了解。

Typically these apps stops pinging the other IPs as soon as they find at least three IP answering to the ping (IOW: they only display the latency / IPs of the first three that did answer). 通常,这些应用程序会在发现至少三个IP应答ping时停止ping其他IP(IOW:它们仅显示前三个确实应答的延迟/ IP)。

The advantage of doing such a check instead of lamely just trying to fetch "the resource you want" (should there be a unique resource you want) is that you can directly see if you have no Internet connection at all or just a problem issuing some particular resource on a particular server. 做这种检查的优势而不是蹩脚只是试图获取“你想要的资源”(如果有你想要的独特资源)是你可以直接看到你是否没有互联网连接或只是发出一些问题特定服务器上的特定资源。

Later on, when you want to fectch a particular resource, you can display message such as : 稍后,当您想要创建特定资源时,可以显示以下消息:

  • you have access to the Internet, but server 'x' seems to be inaccessible 您可以访问Internet,但服务器“x”似乎无法访问

or: 要么:

  • you don't seem to be connected to the Internet 你似乎没有连接到互联网

Which are two TOTALLY different things. 这是两个完全不同的东西。

您可以尝试从网络中检索资源或从网络上检索几乎保证100%正常运行时间的其他小项目,即http://www.google.com.au/favicon.ico或类似

The question seems unclear. 这个问题似乎不清楚。 How will the UI work if the internet connection is not available? 如果互联网连接不可用,UI将如何工作? Unless you are accessing the UI from the same machine as that of the server or in an intranet. 除非您从与服务器或Intranet相同的计算机访问UI。

Do you want to check if the internet connection is available on the client side (in browser)? 您想检查客户端(浏览器)中的Internet连接是否可用吗?

Like @Stephen said above, the only way to determine this is to obtain the remote resource you want . 就像上面说的@Stephen一样,确定这个的唯一方法是获取你想要的远程资源。 On the server side, you can check if the server that hosts your resources is reachable with a combination of these APIs InetAddress.getByName and InetAddress.isreachable 在服务器端,您可以检查托管资源的服务器是否可以通过这些API的组合访问InetAddress.getByNameInetAddress.isreachable

If you want to determine if your clients are online then check with method on the navigator object: 如果要确定客户端是否在线,请检查导航器对象上的方法:

nagivator.onLine

So instead of failing with a bad exception, I would like to fail gracefully. 因此,我希望优雅地失败,而不是失败。

So catch the 'bad' exception and turn it into a graceful failure. 因此,抓住“坏”异常并将其变成优雅的失败。

Some of the other answers here seem to me to be beside the point. 在我看来,其他一些答案似乎与我无关。 Testing connectivity to a site you're never going to connect to is pointless. 测试与您永远不会连接的站点的连接是没有意义的。 Just attempt the connections you want to make and handle failures gracefully. 只需尝试您想要建立的连接并优雅地处理故障。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果没有可用的互联网连接,则我的应用中存在ANR - ANR in my app if no internet connection available 无法连接互联网时应用崩溃 - App crashing when internet connection is not available 如何区分在没有互联网连接的情况下抛出的异常与没有可用服务器/或错误 url 的情况? - How to diferentiate the exception thrown in case of no internet connection from the case of no server available / or wrong url? 用户离线时如何使用自定义的“无Internet连接可用”布局? - How to use a custom No Internet Connection Available layout when user is offline? 如何确定,如果互联网连接当前可用且在Android设备上有效? - how to determine, if an internet connection is currently available and active on an android device? 如何确定JMS连接是否存在? - How to find out if JMS Connection is there? Android应用中没有互联网连接 - No internet connection in android app 如果没有互联网连接,则“重启”应用 - “restart” app if no internet connection Webview 应用程序检查网络并在没有网络时显示“无互联网连接”,并在网络可用时重新加载 - Webview app to check for network and display “no internet connection” if there is none and also reload when network is available 如何在我的 web 查看应用程序中添加无 inte.net 连接页面? - How to add no internet connection page in my web view application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM