问题描述
我正在尝试使用Java的URL
或URI
类解析以问号?
开头的相对链接.
I'm trying to resolve a relative link that starts with a question mark ?
using Java's URL
or URI
classes.
HTML示例:
<a href="?test=xyz">Test XYZ</a>
代码示例(来自Scala REPL):
Code examples (from Scala REPL):
import java.net._
scala> new URL(new URL("http://abc.com.br/index.php?hello=world"), "?test=xyz").toExternalForm()
res30: String = http://abc.com.br/?test=xyz
scala> (new URI("http://abc.com.br/index.php?hello=world")).resolve("?test=xyz").toString
res31: java.net.URI = http://abc.com.br/?test=xyz
问题是浏览器(在Chrome,Firefox和Safari上经过测试)输出了以下URL:http://abc.com.br/index.php?hello=world
.它不会丢弃路径"index.php".它只是替换了查询字符串部分.
The problem is that browsers (tested on Chrome, Firefox and Safari) output the following URL instead: http://abc.com.br/index.php?hello=world
. It doesn't discard the path "index.php". It just replaces the query string part.
似乎浏览器只是遵循 https://stackoverflow.com/a/7872230/40876 .
Jsoup 库在使用element.absUrl("href")
时会产生相同的错误",因为它也依赖于Java的解决.
Jsoup library makes the same "mistake" when we use element.absUrl("href")
as it also depends on java's URL
resolving.
那么Java的URL/URI
解析相对路径是怎么回事?这是错误的/不完整的吗?如何使其行为与浏览器实现相同?
So what's up with java's URL/URI
resolving relative paths? Is it wrong/incomplete?How to make it behave the same as the browsers implementation?
推荐答案
这将很好地工作:
public static void main(String[] args) throws Exception {
String base = "http://abc.com.br/index.php?hello=world";
String relative = "?test=xyz";
System.out.println(new URL(new URL(base), relative).toExternalForm());
// http://abc.com.br/?test=xyz
System.out.println((new URI(base)).resolve(relative).toString());
// http://abc.com.br/?test=xyz
System.out.println(org.apache.http.client.utils.URIUtils.resolve(new URI(base), relative).toString());
// http://abc.com.br/index.php?test=xyz
}
URIUtils位于org.apache.httpcomponents:httpclient 4.0或更高版本中.
URIUtils live in org.apache.httpcomponents:httpclient version 4.0 or higher.
这篇关于Java的URL/URI无法正确解析以?开头的链接(审讯点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!