Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An example of URL which cannot be converted .toURI()?

Tags:

java

url

uri

The javadoc for URL's .toURI() method mentions that it can throw a URISyntaxException if:

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

However, I have so far unsuccessfully tried to trigger this exception. What is more, you need to "pass" the URL constructor tests anyway.

So, does there exist an example of a valid URL which is not a valid URI?

like image 905
fge Avatar asked Apr 30 '14 09:04

fge


2 Answers

The URL class is particularly forgiving when it comes to tolerating syntactically incorrect URLs. If I remember correctly, it only validates if there is a known protocol handler for the scheme and accepts everything in the scheme specific part.

The easiest way to produce a URISyntaxException is probably something like this:

new URL("http:// ").toURI();
like image 142
jarnbjo Avatar answered Sep 23 '22 17:09

jarnbjo


It may contain any of the following characters:

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

Any other character needs to be encoded with the percent-encoding (%hh). Each part of the URI has further restrictions about what characters need to be represented by an percent-encoded word.

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

like image 22
niiraj874u Avatar answered Sep 22 '22 17:09

niiraj874u