简体   繁体   English

java.net.URI.create异常

[英]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 更新1

although 虽然

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

also works perfectly. 也完美。

What's the reason? 什么原因?

The constructor of URI that takes a single String argument requires that you follow the strict syntax rules that RFC 2396 defines for URIs. 带有单个String参数的URI的构造函数要求您遵循RFC 2396为URI定义的严格语法规则。 According to those rules | 根据这些规则| should be encoded as %7C . 应该编码为%7C The other constructors can encode the URI components for you, so for example this won't throw an exception: 其他构造函数可以为您编码URI组件,因此例如,这不会引发异常:

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. 另一方面,URL类不强制执行URI语法规则。 In fact, it is your responsibility to encode the components that should be encoded; 实际上,您有责任对应编码的组件进行编码。 the URL class won't help you. URL类不会帮助您。 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. 调用方有责任对在调用URL之前需要转义的所有字段进行编码,并对从URL返回的所有转义的字段进行解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM