views:

32

answers:

2

i try to url validation. but UrlValidator is does not support unicode. here is code

public static boolean isValidHttpUrl(String url) {
    String[] schemes = {"http", "https"};
    UrlValidator urlValidator = new UrlValidator(schemes);
    if (urlValidator.isValid(url)) {
        System.out.println("url is valid");
        return true;
    }
    System.out.println("url is invalid");
    return false;
}

String url = "ftp://hi.com";
boolean isValid = isValidHttpUrl(url);
assertFalse(isValid);

url = "http:// hi.com";
isValid = isValidHttpUrl(url);
assertFalse(isValid);

url = "http://hi.com";
isValid = isValidHttpUrl(url);
assertTrue(isValid);

// this is problem... it's not true... 
url = "http://안녕.com";
isValid = isValidHttpUrl(url);
assertTrue(isValid);

do you know any alternative url validator support unicode?

i add some case... http://seapy_hi.com is invalid. why? underbar is valid domain why invalid?

A: 

There may be a more recent RFC that supersedes this one, but technically speaking URLs do not suppor Unicode. RFC1738

The relevant section in particular:

No corresponding graphic US-ASCII:

URLs are written only with the graphic printable characters of the
US-ASCII coded character set. The octets 80-FF hexadecimal are not
used in US-ASCII, and the octets 00-1F and 7F hexadecimal represent
control characters; these must be encoded.

Matt
+2  A: 

It doesn't support IDN. You need to convert URL to Punycode first. Try this,

  isValid = isValidHttpUrl(IDN.toASCII(url));
ZZ Coder
it's good. i add some case underbar ... it's invalid why?
seapy
Underscore is not allowed in domain name.
ZZ Coder
um... but some subdomain avaliable.. ex) iloveyou_too.blog.me
seapy