Update on URL Validator for Java

648 Views Asked by At

I know this question has been asked but has anyone found a better URL validator library or tool other then the Apache commons-validator URLValidator, using a URLConnection object (super expensive), or a regex solution? I've tried implementing all of these and it seems the best one is the apache library but it still rejects valid URLs formatted such as:

http://.youtube.com/watch?v=7lu3xMj0HfQ&sns=fb

http://.handelsblatt.com/11635696.html

http://1.kopiyka.club/index/oholoshenya/dim-i-sad/11285.html

I also tired to use google's Guava library but this was not great either. Any suggestions would be appreciated.

In the case of curiosity this is how I implemented the Apache library version:

public boolean validateUrl(final String url)
{
    final String[] urlSchemes = null;
    boolean isValid = false;

    final UrlValidator urlValidator = new UrlValidator(urlSchemes, UrlValidator.ALLOW_ALL_SCHEMES);
    final String urlText = url;
    if(isValid = urlValidator.isValid(urlText))
    {
        //do nothing valid URL
    }
    else
    {
        System.out.printf("URL %s is invalid.", urlText);
        System.out.println();
    }

    return isValid;
}
0

There are 0 best solutions below