我将两个 url 与以下代码合并。
String strUrl1 = "http://www.domainname.com/path1/2012/04/25/file.php";
String arg = "?page=2";
URL url1;
try {
url1 = new URL(strUrl1);
URL reconUrl1 = new URL(url1,arg);
System.out.println(" url : " + reconUrl1.toString());
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
我对结果感到惊讶: http: //www.domainname.com/path1/2012/04/25/?page=2
我希望它是(浏览器做什么):http ://www.domainname.com/path1/2012/04/25/file.php?page=2
关于构造函数 URL(URL 上下文,字符串规范)的 javadoc 解释它应该尊重 RFC。
我做错了什么?
谢谢
更新 :
This is the only problem I encountered with the fonction.
The code already works in all others cases, like browser do
"domain.com/folder/sub" + "/test" -> "domain.com/test"
"domain.com/folder/sub/" + "test" -> "domain.com/folder/sub/test"
"domain.com/folder/sub/" + "../test" -> "domain.com/folder/test"
...