Why is UrlValidator not working for some of my Url?

String[] schemes = {"http","https"}; UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.ALLOW_ALL_SCHEMES); System.out.println(urlValidator.isValid(myUrl)); 

The URL below is not valid. Anyone know why this is so. a local area network is a local area network. But it works for any other public network (it seems).

 http://aunt.localnet/songs/barnbeat.ogg 
+4
source share
7 answers

As I thought, his failure is on the upper level;

 String topLevel = domainSegment[segmentCount - 1]; if (topLevel.length() < 2 || topLevel.length() > 4) { return false; } 

your top level is localnet .

+3
source

The class you are using is deprecated. Replacement

org.apache.commons.validator.routines.UrlValidator

Which is more flexible. You can pass the ALLOW_LOCAL_URLS flag to the constructor, which will allow most addresses to be used, such as the one you are using. In our case, we had authentication data preceding the address, so we had to use an even more flexible UrlValidator (RegexValidator authorityValidator, long options) constructor.

+4
source

This is fixed in version 1.4.1 for Apache Validator:

https://issues.apache.org/jira/browse/VALIDATOR-342 https://issues.apache.org/jira/browse/VALIDATOR/fixforversion/12320156

A simple upgrade to the latest version of the validator should fix the situation.

+1
source

check line 2 it should be

 new UrlValidator(schemes); 

if you want to allow 2 slashes and forbid fragments

 new UrlValidator(schemes, ALLOW_2_SLASHES + NO_FRAGMENTS); 
0
source

Here is the source code for the isValid (String) method:

You can check the result at each step manually to understand where it does not work.

0
source

The library method does not work at this URL:

  "http://en.wikipedia.org/wiki/3,2,1..._Frankie_Go_Boom" 

Which is a perfectly legitimate ([and existing) URL (try it, the StackOverflow dialog didn't accept it, but if you copy it to your browser, you will see the Wikipedia page)

As a result of trial and error, I found that the code below is more accurate:

 public static boolean isValidURL(String url) { URL u = null; try { u = new URL(url); } catch (MalformedURLException e) { return false; } try { u.toURI(); } catch (URISyntaxException e) { return false; } return true; } 
0
source

You can use the following:

 UrlValidator urlValidator = new UrlValidator(schemes, new RegexValidator("^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$"), 0L); 
0
source

Source: https://habr.com/ru/post/1444790/


All Articles