Question

I am trying to check if some host is reachable using the "isReachable" method.

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):

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!

Was it helpful?

Solution

You need to remove the http:// prefix.

As far as I know the InetAddress.getByName() method takes a hostname not a 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. It uses mostly ICMP techniques, which a lot of websites or intermediate firewalls might block.

OTHER TIPS

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. If a literal IP address is supplied, only the validity of the address format is checked.

You don't need to specify scheme. Just remove "http://" and it should work.

did you parsed the url as it should ? you need to get the host name only from 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);

Adding the entry in /etc/hosts with relevant hostname & ip address resolved in my case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top