Ping 与 HTTP HEAD
我正在编写一个 Java 应用程序,它具有通过定期尝试访问服务器来检查它是否连接到互联网的功能。我的第一个想法是 Ping 服务器 - 但结果证明用 Java 实现起来很复杂。因此,我将其重新设计为发送 HTTP HEAD 请求并检查 HTTP 响应代码。我有两个问题:
1)HTTP HEAD 请求与 ping 一样“可靠”吗?我自然会选择 Ping 来检查是否有可用的东西。也许只是因为它很容易在命令行上运行。
2) 如果我向第三方网站发送 HTTP HEAD 请求以检查其是否可访问,是否有一些标准的发送频率?例如,如果我每秒发送一次,我是否会灰心丧气,甚至无法使用这些服务?
I'm writing a Java app which has a feature to check whether it's connected to the internet by periodically trying to access a server. My first idea was to Ping the server - but turned out to be complicated to achieve in Java. So I remade it to send HTTP HEAD requests and check for the HTTP response code instead. I have two questions:
1) Are HTTP HEAD requests "as reliable" as pings? Ping would be my first natural choice to check if something is available. Maybe just because it's so easy to run on the command line.
2) If I send HTTP HEAD requests to a third-party website to check if it is accessible, is there some standard frequency at which these should be sent? Eg if I send them every second, would that be discouraged or even get me blocked from those services?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HTTP HEAD 通常比 ping 更可靠,因为 ICMP 连接通常被阻止,而 HTTP 通常是开放的。每秒检查一次连接听起来相当过分,但这实际上取决于您的用例您尝试“ping”哪个第三方站点。
An HTTP HEAD is generally more reliable than a ping, as ICMP connections are often blocked and HTTP is usually open. Checking for a connectivity every second sounds pretty excessive, but it really depends on your use case what third party site you are trying to "ping".
我无法评论是否使用 HEAD 更有效,或者尝试执行诸如删除系统并执行 ping 之类的操作;但我认为它们都不是您应该做的解决方案。恕我直言,没有必要轮询您的连接。在很多情况下,连接可能会被断开,我认为轮询不会提供太多缓解问题的方法。此外,用户可能会感到恼火。我知道,如果我正在使用一个应用程序,然后开始做其他事情,突然间我从一个我什至没有注意到的应用程序中收到了“连接因第 3 方错误而丢失”;我会很生气。
如果您的应用程序依赖于存在的连接,那么我认为使用异常处理程序来处理此问题是公平的。我敢打赌,无论您使用什么 API,每当您尝试网络操作并且无法建立连接时,都会抛出某种异常。因此,我要做的是在任何初始化网络操作的类中,我都会遵循以下范例:
您的应用程序不应该能够确定连接何时丢失,而只是在尝试网络操作时如何做出反应,并且没有找到连接。
话虽这么说——你可能仍然不同意我的观点。如果是这种情况,那么允许/推荐的轮询频率可能会记录在您正在使用的服务的 API 中。此外,如果来自第 3 方的资源是静态的,您应该缓存它,而不是一遍又一遍地获取它。
I can't comment on whether its more effective to use HEAD or trying to do something like drop to the system and do a ping; but I don't think either of them is the solution you should be doing. IMHO, it isn't necessary to poll your connection. There are a lot of situations where a connection could be dropped and I don't think polling will provide much in the way of mitigating the problem. Also, the user might be annoyed. I know that if I were using an application, and then started doing something else, and all of a sudden I got a "connection lost to 3rd party error" from an application I wasn't even paying attention to; I would be very annoyed.
If your application depends on a connection being present, then I think its fair to handle this with exception handlers. I'm willing to be bet that whatever API you're using throws some kind of exception whenever you attempt a network action and you aren't able to establish a connection. So, what I would do is in whatever class you're initializing the network action, I would follow this paradigm:
Your application shouldn't be able to determine when a connection was lost, just how to react when you attempt a network action and no connection is found.
That being said - you still may disagree with me. If that is the case then the frequency of polling allowed/recommended might be documented in the API for the service you're using. Also, if the resource from the 3rd party is static, you should cache it as opposed to getting it over and over again.
我想扩展 @Dave 的答案,评论是不够的。
java.io.IOException
。查看 IOException 的子类 - UnknownHostException、SocketException 和 ProtocolException 通常带有“无 Internet 连接”的味道I wanted to expand @Dave's answer, and a comment would not suffice.
java.io.IOException
is thrown. Look at the sub-classes of IOException - UnknownHostException, SocketException and ProtocolException are the ones that usually smell of "No Internet Connection"