Skip to content

Instantly share code, notes, and snippets.

@spencergibb
Last active December 17, 2015 23:12
Show Gist options
  • Save spencergibb/ea2e582d571f97297e4d to your computer and use it in GitHub Desktop.
Save spencergibb/ea2e582d571f97297e4d to your computer and use it in GitHub Desktop.
What URI thinks are valid hosts
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());
}
}
}
@spencergibb
Copy link
Author

Results:

URI ================================
www.example.com
example
example9
example-dash
Error: example_underscore is not a valid hostname.
Error: _example_underscore is not a valid hostname.
Error: -example-dash is not a valid hostname.
Error: bücher is not a valid hostname.


InetAddress ========================
93.184.216.34
Error: example: unknown error
Error: example9: unknown error
Error: example-dash: unknown error
Error: example_underscore: unknown error
Error: _example_underscore: unknown error
Error: -example-dash: unknown error
Error: bücher: unknown error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment