Example url that cannot be converted to .toURI ()?

The javadoc for the URL .toURI() method mentions that it can URISyntaxException if:

[...] this URL is not formatted strictly in accordance with RFC2396 and cannot be converted to a URI.

However, I have so far unsuccessfully tried to raise this exception. Moreover, you need to “pass” the tests of the URL constructor anyway.

So, is there an example of a valid URL that is not a valid URI ?

+6
source share
2 answers

The URL class is especially simple when it comes to tolerance for syntactically invalid URLs. If I remember correctly, it only checks if a known protocol exists for the circuit and accepts everything in a specific part of the circuit.

The easiest way to create a URISyntaxException is something like this:

 new URL("http:// ").toURI(); 
+4
source

It can contain any of the following characters:

  ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=. 

Any other character must be encoded using percent encoding (% hh). Each part of the URI has additional restrictions as to which characters should be represented by a percent encoded word.

It is also true that different symbols are legal at different points. For example, according to RFC 2396, unescaped '?' is legal in part of the fragment, but not in part of the path.

+2
source

All Articles