2012-04-27 215 views
2

我使用以下代码合并两个URL。在JAVA中合并两个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

临屋的javadoc关于构造URL(网址背景下,字符串规范)解释它应该尊重RFC。

我做错了什么?

感谢

UPDATE:

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" 
    ... 

回答

2

你总是可以先合并字符串,然后创建基于合并的字符串的URL。

StringBuffer buf = new StringBuffer(); 
    buf.append(strURL1); 
    buf.append(arg); 
    URL url1 = new URL(buf.toString()); 
+0

它更复杂,因为代码应该能够处理所有其他情况下,已经为我添加URL所梅索德处理我的问题 – benfromaix 2012-04-27 14:15:12

+0

在这种情况下,我认为@Anurag Ramdasan/aioobe答案更适合你。基本上URLContext将删除“file.php”。但只要你能够减去这个值并将其用作URL参数,你就可以走了。 – Rudy 2012-04-30 07:20:22

1

尝试

String k = url1+arg; 
URL url1; 
    try { 
     url1 = new URL(k); 
     //URL reconUrl1 = new URL(url1,arg); 
     System.out.println(" url : " + url1.toString()); 
    } catch (MalformedURLException ex) { 
     ex.printStackTrace(); 
    } 
+0

它更复杂,因为代码应该能够处理URL方法已处理的所有其他案例,正如我在我的问题 – benfromaix 2012-04-27 14:28:49

+0

中添加的,但代码应该能够处理所有其他已由URL方法处理的案例,正如我在我的题。 – benfromaix 2012-04-27 14:51:46

1

我还没有读完the RFC,但背景(如Java文档提到URL)大概是一个URL的目录中,这意味着的

"http://www.domainname.com/path1/2012/04/25/file.php" 

上下文

"http://www.domainname.com/path1/2012/04/25/" 

这就是为什么

new URL(url1,arg); 

产生

"http://www.domainname.com/path1/2012/04/25/?page=2" 

“变通办法” 显然是对自己的拼接部分,采用+

1

您正在使用URL的构造函数,其参数为URL(URL context, String spec)。所以你不要通过网址传递php页面,而是使用字符串。上下文需要成为目录。要做到这一点,正确的方法是

String strUrl1 = "http://www.domainname.com/path1/2012/04/25"; 
    String arg = "/file.php?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(); 
} 
+0

好吧,但是当我得到一个页面的html源代码时,我必须处理所有的情况。我有提供源代码的页面的URL,以及指向外部页面的所有情况下的链接(相对链接,绝对链接,查询...)是否有这样的功能? – benfromaix 2012-04-27 10:33:05

+0

如果要从URL中检索源,最简单的方法是使用HttpURLConnection类。它将响应作为页面的来源发送。 – 2012-04-27 10:40:26

+0

我的问题与路径和它的上下文之间的url解析有关。 – benfromaix 2012-04-27 14:51:17

0

当你读它提到有关指定URL
哪个域和路径的背景下,Java文档试试这个

String strUrl1 = "http://www.domainname.com/path1/2012/04/25/"; 
     String arg = "file.php?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(); 
     } 
0

"http://www.domainname.com" + "/path1/2012/04/25/" 

其中"file.php"被认为是属于上述上下文的文本。
这两个参数重载构造函数使用URL的上下文作为基础,并添加第二个参数来创建一个完整的URL,这不是你所需要的。

所以最好以字符串添加两个部分,然后从它们创建网址:

String contextURL = "http://www.domainname.com/path1/2012/04/25/"; 
    String textURL = "file.php?page=2"; 
    URL url; 
    try { 
     url = new URL(contextURL); 
     URL reconUrl = new URL(url, textURL); 
     System.out.println(" url : " + reconUrl.toString()); 
    } catch (MalformedURLException murle) { 
     murle.printStackTrace(); 
    } 
+0

它更复杂,因为代码应该能够处理URL方法已处理的所有其他案例,正如我在我的问题中添加的那样 – benfromaix 2012-04-27 14:29:58

+0

然后制作一个方法,将URL(url,textURL)等所需的两个URL组合起来;你的目标将会实现! – GingerHead 2012-04-27 16:04:38