-1

I have a problem. I want to display all address by URL, kind https://google.com, but I get an error:

Exception in thread "main" java.net.UnknownHostException: This host is unknown (https://google.com) I know that by entering a regular domain (like google.com) into getAllByNAME , everything will work. Help me solve this problem please

My code:

public class Main {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress[] inetAddress = InetAddress.getAllByName("https://google.com");
        for (InetAddress key : inetAddress){
            System.out.println(key.getHostAddress());
        }
    }
}
panic08
  • 87
  • 1
  • 7
  • 1
    *"I know that by entering a regular domain (like google.com) into getAllByNAME , everything will work."* - It sounds like you've already found the solution... ? – David Apr 03 '23 at 14:18
  • @David but I need to insert a URL into this method, not a regular domain – panic08 Apr 03 '23 at 14:19
  • 2
    [The documentation](https://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#getAllByName(java.lang.String)) specifies that the method expects a *host name*, not a complete URL. What you have here is called an [XY Problem](https://xyproblem.info/). You'll need to address why you **think** you "need" to do this and adjust your approach accordingly. For example, you can write your own method which accepts a complete URL, parses out the host name, and uses that host name for this functionality. – David Apr 03 '23 at 14:21

1 Answers1

2

You are giving a complete URI with protocol (https://) while only the domain is requested. The function is basically trying to resolve something that is not a domain name, so it does not work.

You can only use it with domain names (so the same as your URI, but without the protocol), like you pointed out with google.com.

If you need to split the URI, you can use the split function, or do as explained here : Java URL Without Protocol