如何在 Java 中测试互联网的可用性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/644856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:11:48  来源:igfitidea点击:

How do I test the availability of the internet in Java?

java

提问by Penchant

I don't want to tell the hard way that the internet is unavailable when I catch an exception from url.openStream().

当我从 url.openStream() 捕获异常时,我不想硬说互联网不可用。

Is there a simple way to tell if the computer is connected to the internet in Java? In this scenario, "connected to the internet" means being able to download data from a specific url.

有没有一种简单的方法可以判断计算机是否已在 Java 中连接到 Internet?在这种情况下,“连接到互联网”意味着能够从特定 url 下载数据。

If I try to download from it and it is not available, then the program hangs for a bit. I dont want that hanging. Therefore, i need a fast way of querying whether the website is available or not.

如果我尝试从它下载​​但它不可用,那么程序会挂起一段时间。我不想要那个挂。因此,我需要一种快速查询网站是否可用的方法。

回答by Steve Weet

The problem you are trying to avoid is waiting for for your http connection to determine that the URL you are trying to access is really unavailable. In order to achieve this you need to stop using url.openStream() which is a shortcut for openConnection().getInputStream() and get some finer control over your connection.

您试图避免的问题是等待您的 http 连接确定您尝试访问的 URL 确实不可用。为了实现这一点,您需要停止使用 url.openStream(),它是 openConnection().getInputStream() 的快捷方式,并更好地控制您的连接。

URLConnection conn = url.openConnection();  
conn.setConnectTimeout(timeoutMs);  
conn.setReadTimeout(timeoutMs);  
in = conn.getInputStream();  

This code will allow you to timeout the connection attempt if either the connection or the read exceeds the time you provide in the timeoutMs paramater.

如果连接或读取超过您在 timeoutMs 参数中提供的时间,此代码将允许您超时连接尝试。

回答by Naga Rishi

Use

利用

Process p1 = java.lang.Runtime.getRuntime().exec("ping www.google.com");
System.out.println(p1.waitFor());
// return code for p1 will be 0 if internet is connected, else it will be 1

回答by Anonymous

There is no such thing as "Internet availability". Imagine that your HTTP request go through a transparent HTTP proxy, you are trying to access a blacklisted site and you get a HTTP response from the proxy server telling you about access denied. Is Internet available in this scenario or not?

没有“互联网可用性”这样的东西。想象一下,您的 HTTP 请求通过一个透明的 HTTP 代理,您正在尝试访问一个列入黑名单的站点,并且您从代理服务器收到一个 HTTP 响应,告诉您访问被拒绝。在这种情况下互联网是否可用?

I think you shall be more specific about your problem.

我认为你应该更具体地说明你的问题。

回答by cletus

You might be able to tell if a compouter is connected to a network but even if it is there's no guarantee the networking is working or that it's connected to the internet.

您也许能够判断计算机是否已连接到网络,但即使连接到网络,也不能保证网络正常工作或已连接到 Internet。

This is one of those "suck it and see" problems.

这是“吮吸它”的问题之一。

What's wrong with trying to connect? And why are you concerned whether or not they're connected to the internet?

尝试连接有什么问题?为什么你担心他们是否连接到互联网?

回答by Thorbj?rn Ravn Andersen

If the criteria is whether you can access a given web server the only way to find out, is to access that web server.

如果标准是您是否可以访问给定的 Web 服务器,那么唯一的方法就是访问该 Web 服务器。

You can, however, do it in a background thread at start up so the application is not delayed by it. Then the "yes/no" answer is available when the actual downloading is desired. Put a message in the status line so the user knows what is going on and is not suprised about uninitiated "connect to network" if s/he is not connected when your program is started.

但是,您可以在启动时在后台线程中执行此操作,这样应用程序就不会被它延迟。然后,当需要实际下载时,“是/否”答案可用。在状态行中放置一条消息,以便用户知道发生了什么,并且如果在您的程序启动时他/她未连接,则不会对未启动的“连接到网络”感到惊讶。

回答by Chris

I don't know much about the low level HTTP plumbing available in Java, but you could always issue an http HEAD request for the url in advance. This causes the server to send back only the headers and not the data. With a 1-3 second timeout, you should be able to avoid any lengthy delays.

我对 Java 中可用的低级 HTTP 管道了解不多,但您始终可以提前对 url 发出 http HEAD 请求。这会导致服务器仅发回标头而不是数据。通过 1-3 秒的超时,您应该能够避免任何长时间的延迟。

回答by Peter Lawrey

If you program is hanging while waiting to download then you are probably using the GUI thread to do the download. If you use a background thread, your program won't hang even if it take a long time to timeout/download.

如果您的程序在等待下载时挂起,那么您可能正在使用 GUI 线程进行下载。如果您使用后台线程,即使超时/下载需要很长时间,您的程序也不会挂起。

回答by dicroce

You could send an ICMP message to a very well known host...

您可以向一个非常知名的主机发送 ICMP 消息...

回答by FerranB

You can check for a NTP serverif your firewall does not forbids.

如果您的防火墙没有禁止,您可以检查NTP 服务器

Any way I think the best option is to try to open an URL as you try to avoid, because is the service most commonly to be opened.

无论如何,我认为最好的选择是尽量避免打开 URL,因为这是最常打开的服务。

You can check for one or two dynamic URL that change constantly. For instace, some web that shows time. For instance this onei found googleing. More than one because the sites can be down ;-)

您可以检查一两个不断变化的动态 URL。例如,一些显示时间的网络。例如这个我发现谷歌搜索。不止一个,因为网站可能会关闭;-)