java.net.URL 和相对 url
我的 URL 和相对路径(查询)有问题。我编写此代码是为了从相对 url 获取绝对 url:
old = "http://domain/script?param";
new_ = "?otherparam";
URL u = new URL(old);
u = new URL(u,new_);
这是输出:
JAVA URL: http://domain/script?param + ?otherparam = http://domain/?otherparam
FireFox: http://domain/script?param + ?otherparam = http://domain/script?otherparam
Why does URL's result different from FireFox?如何像 FireFox 一样构建 URL?
I have a problem with URL and relative path (query). I wrote this code to get absolute url from relative url:
old = "http://domain/script?param";
new_ = "?otherparam";
URL u = new URL(old);
u = new URL(u,new_);
Here is output:
JAVA URL: http://domain/script?param + ?otherparam = http://domain/?otherparam
FireFox: http://domain/script?param + ?otherparam = http://domain/script?otherparam
Why does URL's result differ from FireFox? How to build URL like FireFox does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java 中的 BUG #6519518:当仅查询相对规范时,URL 错误地删除了路径叶 ( RFC1808)
描述包含解决方法。
It's BUG #6519518 in Java: URL incorrectly removes path leaf when the relative spec is query only (RFC1808)
The description contains a workaround.
阅读 URL(URL context, String spec) 的 javadoc 为您的问题提供了最佳答案:
由于您的 URL 上下文 URL 结尾没有斜杠,因此它会被删除。
尝试添加斜杠:
old = "http://domain/script/?param";
Reading javadoc of URL(URL context, String spec) provides the best answer to your question:
Since your URL context URL ends without slash, it's getting removed.
Try to add slash:
old = "http://domain/script/?param";