2

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?

1
  • yes, Firefox or any other browser. Commented Jan 3, 2011 at 9:57

2 Answers 2

3

It's BUG #6519518 in Java: URL incorrectly removes path leaf when the relative spec is query only (RFC1808)

The description contains a workaround.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I see that no ready solution for my task. I will port my own c++ code.
1

Reading javadoc of URL(URL context, String spec) provides the best answer to your question:

If the spec's path component begins with a slash character "/" then the path is treated as absolute and the spec path replaces the context path.

Otherwise, the path is treated as a relative path and is appended to the context path, as described in RFC2396. Also, in this case, the path is canonicalized through the removal of directory changes made by occurences of ".." and ".".

Since your URL context URL ends without slash, it's getting removed.

Try to add slash: old = "http://domain/script/?param";

1 Comment

I'm making sitemap. I need to download each page and extract all urls (a href) from it. Urls are different - start with ?, /, ./, ../, absolute, etc. Need to process them all. Sorry, I didn't get your best answer. In my sample url starts with "?" (it's query part).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.