2

I merge two url with the following code.

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();
    }

I'm surprise by the result : http://www.domainname.com/path1/2012/04/25/?page=2

I expect it to be (what browser do) : http://www.domainname.com/path1/2012/04/25/file.php?page=2

Tha javadoc about the constructor URL(URL context, String spec) explain it should respect the RFC.

I'm doing something wrong ?

Thanks

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"
  ...
benfromaix
  • 105
  • 1
  • 3
  • 9

6 Answers6

1

You can always merge the String first and then created the URL based on the merged String.

  StringBuffer buf = new StringBuffer();
  buf.append(strURL1);
  buf.append(arg);
  URL url1 = new URL(buf.toString());
Rudy
  • 7,008
  • 12
  • 50
  • 85
  • It's more complicated because the code should be able to handle all other case already handle by the URL methode as I added in my question – benfromaix Apr 27 '12 at 14:15
  • In that case I think @Anurag Ramdasan / aioobe answer is more suitable to you. Basically URLContext will remove the "file.php". But as long as you able to subtract this and use it as a URL parameter, you are good to go. – Rudy Apr 30 '12 at 07:20
1

try

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();
    }
Satya
  • 8,693
  • 5
  • 34
  • 55
  • It's more complicated because the code should be able to handle all other case already handle by the URL methode as I added in my question – benfromaix Apr 27 '12 at 14:28
  • but the code should be able to handle all other case already handle by the URL methode as I added in my question. – benfromaix Apr 27 '12 at 14:51
1

I haven't read through the RFC, but the context (as mentioned in the Java Doc for URL) is presumably the directory of a URL, which means that the context of

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

is

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

which is why

new URL(url1,arg);

yields

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

The "workaround" is obviously to concatenate the parts yourself, using +.

aioobe
  • 413,195
  • 112
  • 811
  • 826
1

you are using the constructor of URL here which takes paramter as URL(URL context, String spec). So you dont pass the php page with the URL but instead with the string. context needs to be the directory. the proper way to do this would be

  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();
 }
Anurag Ramdasan
  • 4,189
  • 4
  • 31
  • 53
  • Ok, but when I get the html source code of a page, I have to handle every case. I have the url of the page that provide the source code, and the link pointing to the external page with all cases (relative link, absolute link, query...) Is there a function for that ? – benfromaix Apr 27 '12 at 10:33
  • if you want to retrieve the source from the URL the easiest way to do it is using the HttpURLConnection class. it sends response as the source of the page. – Anurag Ramdasan Apr 27 '12 at 10:40
  • My question is related to the url resolve between a path and it context. – benfromaix Apr 27 '12 at 14:51
0

Try this

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();
        }
Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40
0

When you read the java doc it mentions about the context of the specified URL
Which is the domain and the path:

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

Where "file.php" is considered the text where it belongs to the context mentioned above.
This two parameter overloaded constructor uses the context of a URL as base and adds the second param to create a complete URL, which is not what you need.

So it's better to String add the two parts and then create URL from them:

   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();
    }
GingerHead
  • 8,130
  • 15
  • 59
  • 93
  • It's more complicated because the code should be able to handle all other case already handle by the URL methode as I added in my question – benfromaix Apr 27 '12 at 14:29
  • Then mke a method that combines the needed two URLs just like URL(url, textURL); and your purpose will be accomplished! – GingerHead Apr 27 '12 at 16:04