Archer
Archer

Reputation: 5147

java.net.URI.create exception

java.net.URI.create("http://adserver.adtech.de/adlink|3.0")

throws

java.net.URISyntaxException: 
Illegal character in path at index 32: http://adserver.adtech.de/adlink|3.0

although

new java.net.URL("http://adserver.adtech.de/adlink|3.0")

works just fine.

UPDATE 1

although

new org.apache.commons.httpclient.URI("http://adserver.adtech.de/adlink|3.0")

also works perfectly.

What's the reason?

Upvotes: 2

Views: 7993

Answers (1)

Joni
Joni

Reputation: 111239

The constructor of URI that takes a single String argument requires that you follow the strict syntax rules that RFC 2396 defines for URIs. According to those rules | should be encoded as %7C. The other constructors can encode the URI components for you, so for example this won't throw an exception:

new java.net.URI("http", "//adserver.adtech.de/adlink|3.0", null);

The URL class on the other does not enforce the URI syntax rules. In fact, it is your responsibility to encode the components that should be encoded; the URL class won't help you. From the documentation:

It is the responsibility of the caller to encode any fields, which need to be escaped prior to calling URL, and also to decode any escaped fields, that are returned from URL.

Upvotes: 6

Related Questions