java.net.URI.create("http://adserver.adtech.de/adlink|3.0")
抛出
java.net.URISyntaxException:
Illegal character in path at index 32: http://adserver.adtech.de/adlink|3.0
虽然
new java.net.URL("http://adserver.adtech.de/adlink|3.0")
工作得很好。
更新1
虽然
new org.apache.commons.httpclient.URI("http://adserver.adtech.de/adlink|3.0")
也能完美地工作。
原因是什么?
最佳答案
采用单个字符串参数的 URI 构造函数要求您遵循 RFC 2396 为 URI 定义的严格语法规则。根据这些规则,|
应编码为 %7C
。其他构造函数可以为您编码 URI 组件,因此这不会引发异常:
new java.net.URI("http", "//adserver.adtech.de/adlink|3.0", null);
另一个 URL 类不强制执行 URI 语法规则。事实上,你有责任对应该编码的组件进行编码; URL 类不会帮助你。来自 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.
关于java.net.URI.create 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15487445/