java - 使用 Java 检查系统的互联网连接

标签 java proxy httpurlconnection

我写了一个方法,可以帮助我们判断系统是否使用代理连接互联网,如下,

try {

    System.out.println("Checking internet connection availability.....");
    URL u = new URL("http://www.google.com/");
    HttpURLConnection uc = (HttpURLConnection) u.openConnection();
    uc.setReadTimeout(1);//I have tried this without timeout and with it too. But it didnt work
    System.out.println(uc.getResponseCode());
} catch (Exception ex) {
    System.out.println("Unable to connect to internet without proxy.....");
    System.out.println("Checking for any proxy settings from the PC");
    System.setProperty("java.net.useSystemProxies", "true");
    try {
        System.setProperty("java.net.useSystemProxies", "true");
        URL u = new URL("http://www.google.com/");
        HttpURLConnection uc = (HttpURLConnection) u.openConnection();
        System.out.println(uc.getResponseCode());
        System.out.println("Internet connection available");
    } catch (Exception e) {
        System.out.println("Internet connection not available :(");
    }
}

最初,我尝试在没有代理的情况下打开 URL 连接(假设系统没有代理来连接互联网)。我已将超时设置为 1 毫秒。尝试从站点获取响应代码。 如果发生任何错误(例如超时),则在 catch block 中,我尝试通过将 useSystemProxies 设置为 true 来使用系统代理连接到互联网。 但即使在那之后,我也无法从该网站获得响应。

我正在使用带有代理设置的系统。

我也在 catch block 中尝试了以下操作

Proxy next = ProxySelector.getDefault().select(new URI("http://www.google.com/")).iterator().next();
if (next.address() != null) {
    System.out.println("Detecting Proxy configurations.....");
    String proxy = next.address().toString();
    String proxyHost = proxy.substring(0, proxy.indexOf(":"));
    String proxyPort = proxy.substring(proxy.indexOf(":") + 1);
    System.out.println("Proxy Configuration : " + proxyHost + " @ " + proxyPort);
}

上面的代码块也不起作用。谁能帮我解决这个问题吗?

最佳答案

InetAddress.isReachable

Test whether that address is reachable. Best effort is made by the implementation to try to reach the host, but firewalls and server configuration may block requests resulting in a unreachable status while some specific ports may be accessible. A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host. The timeout value, in milliseconds, indicates the maximum amount of time the try should take. If the operation times out before getting an answer, the host is deemed unreachable. A negative value will result in an IllegalArgumentException being thrown.

关于java - 使用 Java 检查系统的互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15675739/

相关文章:

java - getRequestProperty ("Authorization") 总是返回 null

java - 为什么使用 POST 方法的 Java HttpURLConnections 会导致返回 Http 500 错误?

java - 如何将 Gmail 日期格式转换为 java.util.Date?

java - 使用 JFrame 在 JPanel 之间切换

java - SWT:如何在标签中开始新行?

java - 似乎方法 .connect() 不起作用

java - 在 play 2.0.3 中拒绝表单而不重新加载页面

http - 如何与 Golang 一起使用 nginx 作为反向代理?

json - 如何使用代理接口(interface)在 RestEasy 客户端框架中设置 "Content-type" header ?

java - AmazonS3Client 在 hadoop(oozie) 中通过代理