Last active
December 17, 2015 23:12
-
-
Save spencergibb/ea2e582d571f97297e4d to your computer and use it in GitHub Desktop.
What URI thinks are valid hosts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.net.InetAddress; | |
| import java.net.URI; | |
| public class DomainValidator { | |
| public static String[] hosts = new String[] { | |
| "www.example.com", | |
| "example", | |
| "example9", | |
| "example-dash", | |
| "example_underscore", | |
| "_example_underscore", | |
| "-example-dash", | |
| "bücher", | |
| }; | |
| public static void main(String[] args) { | |
| System.out.println("URI ================================"); | |
| for (String host : hosts) { | |
| printURI(host); | |
| } | |
| System.out.println("\n"); | |
| System.out.println("InetAddress ========================"); | |
| for (String host : hosts) { | |
| printInetAddr(host); | |
| } | |
| } | |
| private static void printURI(String domain) { | |
| URI uri = URI.create("http://" + domain); | |
| if (uri.getHost() == null) { | |
| System.out.println("Error: "+domain + " is not a valid hostname."); | |
| } else { | |
| System.out.println(uri.getHost()); | |
| } | |
| } | |
| private static void printInetAddr(String domain) { | |
| try { | |
| InetAddress addr = InetAddress.getByName(domain); | |
| System.out.println(addr.getCanonicalHostName()); | |
| } catch (Exception e) { | |
| System.out.println("Error: "+ e.getMessage()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: