简体   繁体   English

Java InetAddress失败

[英]Java InetAddress failed

I am trying to check if some host is reachable using the "isReachable" method. 我试图检查是否可以使用“isReachable”方法访问某些主机。

line 113: oaiBaseURL = "http://www.cnn.com";//////////////////////////////////////
line 114: boolean res = InetAddress.getByName(oaiBaseURL).isReachable(10000);
line 115: System.out.println("------reachable:"+res);

and get the following error message (in eclipse): 并获取以下错误消息(在eclipse中):

java.net.UnknownHostException: http://www.cnn.com
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source)
    at java.net.InetAddress.getAddressFromNameService(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getByName(Unknown Source)
    at com.irWizard.web.validator.WizardValidator.validateForm(WizardValidator.java:114)

Does anyone understand what might be the reason for this error? 有谁知道这个错误的原因是什么?

Thanks in advance! 提前致谢!

You need to remove the http:// prefix. 您需要删除http://前缀。

As far as I know the InetAddress.getByName() method takes a hostname not a URL. 据我所知, InetAddress.getByName()方法接受主机名而不是URL。

You can change the code as follows: 您可以按如下方式更改代码:

   URL url = new URL("http://www.cnn.com");
   boolean res = InetAddress.getByName(url.getHost()).isReachable(10000);
   System.out.println("------reachable:"+res);

However keep in mind the mechanisms the method isReachable() uses to determine whether it is reachable or not. 但请记住方法isReachable()用于确定它是否可访问的机制。 It uses mostly ICMP techniques, which a lot of websites or intermediate firewalls might block. 它主要使用ICMP技术,许多网站或中间防火墙可能阻止这些技术。

Quote from documentation : 文档引用:

The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. 主机名可以是机器名称,例如“ java.sun.com”,也可以是其IP地址的文本表示。 If a literal IP address is supplied, only the validity of the address format is checked. 如果提供文字IP地址,则仅检查地址格式的有效性。

You don't need to specify scheme. 您无需指定方案。 Just remove "http://" and it should work. 只需删除“http://”即可。

did you parsed the url as it should ? 您是否已解析网址? you need to get the host name only from URL 您只需要从URL获取主机名

URL oaiBaseURL = new URL("http://www.cnn.com");
boolean res = InetAddress.getByName(oaiBaseURL.getHost()).isReachable(10000);
System.out.println("------reachable:"+res); 

I tried this on my laptop by creating hotspot and it worked. 我通过创建热点在笔记本电脑上进行了尝试,并且可以正常工作。 Sometimes there may be problem in giving the subnet input to the program at that time you may face this problem. 有时在将子网输入提供给程序时可能会出现问题,此时您可能会遇到此问题。 Hope it will work for you. 希望它能为你效劳。

subnet = subnet.trim();
int timeout = 1500;
for(int i=1;i<254;i++)
    {
    try
      {
        String host = subnet +"."+i;
        if (InetAddress.getByName(host).isReachable(timeout))
          {
            Check = Check+host+"\n";
            System.out.println(host);
          } 

       }
    catch (UnknownHostException ex) { 
        Logger.getLogger(WiFi.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(WiFi.class.getName()).log(Level.SEVERE, null, ex);

在我的情况下,在/ etc / hosts中添加具有相关主机名和IP地址的条目已解决。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM