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());
}
}
}
>Solution :
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