Inspired by the code I found in this post , I created the following validator method, which seems to be fairly easy to validate. By reading the JavaDoc URI, I removed some false positives, such as "host: 80" and "hostname / page", but I cannot guarantee that some false positives will remain.
public static boolean isValidHostNameSyntax(String candidateHost) { if (candidateHost.contains("/")) { return false; } try { // WORKAROUND: add any scheme and port to make the resulting URI valid return new URI("my:// userinfo@ " + candidateHost + ":80").getHost() != null; } catch (URISyntaxException e) { return false; } }
Henno vermeulen
source share