代码之家  ›  专栏  ›  技术社区  ›  ken

使用Java检测互联网连接[重复]

  •  35
  • ken  · 技术社区  · 17 年前


    How to check if internet connection is present in java?

    我能想到的唯一方法是尝试打开一个网站的URL连接,如果失败,则返回false。我的另一种方法如下,但我不知道这是否总体稳定。如果我拔下网线,在尝试创建InetAddress时确实会出现UnknownHostException。否则,如果电缆已连接,我将获得一个有效的InetAddress对象。我还没有在mac上测试下面的代码。

    public static boolean isInternetReachable()
            {
                try {
    
                    InetAddress address = InetAddress.getByName("java.sun.com");
    
                    if(address == null)
                    {
                        return false;
                    }
    
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return false;
                }
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
    

    最终代码块:

    //checks for connection to the internet through dummy request
            public static boolean isInternetReachable()
            {
                try {
                    //make a URL to a known source
                    URL url = new URL("http://www.google.com");
    
                    //open a connection to that source
                    HttpURLConnection urlConnect = (HttpURLConnection)url.openConnection();
    
                    //trying to retrieve data from the source. If there
                    //is no connection, this line will fail
                    Object objData = urlConnect.getContent();
    
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return false;
                }
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
    
    7 回复  |  直到 9 年前
        1
  •  17
  •   jsight TaherT    17 年前

    另一件要记住的事情是,您需要设置一个系统属性来关闭java运行时中的dns缓存。否则,它可能会根据缓存的数据继续报告网络已启动(即使它已关闭)。

        2
  •  11
  •   Joseph Earl    16 年前

    HttpURLConnection 是 0

    因此,我建议您检查UI线程,并添加 urlConnect.setConnectTimeout(1000); 之前 召唤 urlConnect.getContent();

    这样你就知道地址是可访问的,下载一个10k的文件不需要5年时间。

        3
  •  3
  •   user95947 user95947    17 年前

        4
  •  2
  •   jptsetme    17 年前

    还没有测试过这个,但我建议看看java.net。NetworkInterface.getNetworkInterfaces()。这将返回计算机上所有网络接口的枚举,如果没有,则返回null。

        5
  •  2
  •   Thorbjørn Ravn Andersen    17 年前

    确定可以访问给定服务的唯一方法是向该服务发出虚拟请求。Ping可能会被防火墙阻止。某些服务器可能可以访问,而其他服务器则无法访问。如果您需要与Web服务通信,请为这些请求返回一个静态页面。

    此外,在尝试联系用户之前,请记住询问用户。

        6
  •  2
  •   andsegu    16 年前

    使用URL连接时,您会遇到一个问题,即您使用的getContent应该获取html,这样您就可以在那里使用数据。如果调用非常频繁,这可能是一个问题(如果运行软件的设备上没有无限的数据计划,情况会更糟)。

        7
  •  1
  •   user207421    16 年前

    创建 一个。仅提到的Windows API告诉您的调制解调器是否已拨号,实际上我已经看到了 原因 拨号,这并不是我们的想法。在过去的8年左右的时间里,我并没有拨号上网。