问题
NOTE: I am asking for real world problem, not for theoretical purpose; see the last part of the question -- the same way browsers do the job.
Usually you would see the answer:
new java.net.URL(new java.net.URL(base_url),rel_url).toString
(base_url and rel_url are String). In my case base_url is the URL of page I fetched, rel_url comes from "<a href=..." value, so it might be even single "#" character (for example).
However such code does not work for URL fragments, like such two pieces:
htpp://www.hello.com/1.html
?p=2
I tested Firefox, Chromium, Opera, Konqueror, "Web Browser" (Gnome modesty ;-D) -- all of them combine those URLs as:
htpp://www.hello.com/1.html?p=2
With code as above I get:
htpp://www.hello.com/?p=2
Question
How do you combine URL fragments, in a ready for world manner?
I hope there is already handy library for that, before I start doing parsing by myself ;-).
回答1:
You are misunderstanding what a URL is. ?p=2 is a query string, not a relative URL. (You may also find #foo, which is usually called a fragment identifier or reference and is most commonly used to jump to a section of a long document). The full scheme for URIs is described on Wikipedia among many other places (you can also find the differences between URIs and URLs in various places).
Anyway, relative URLs refer only to the path part of the URL--it is whether the path is absolute or relative. If you have a query string and wish to attach it to an existing URL (which does not have a query string), just append it to the string. If you don't know whether you have a query string, you can use the methods in the URL class to test for it.
If you want to replicate what browsers do, given a full URL url and a String s,
if (s.startsWith("?") || s.startsWith("#")) new java.net.URL(url.toString + s)
else new java.net.URL(url, s)
should do the trick. (I don't know the exact code that different browsers use, but this replicates the behavior that you describe of appending a query string if that is all that is provided in a href.) If you don't know whether your existing URLs might have query strings or not, then you can
if (s.startsWith("#")) new java.net.URL(url.toString.takeWhile(_ != '#') + s)
else if (s.startsWith("?")) new java.net.URL(url.toString.takeWhile(_ != '?') + s)
else new java.net.URL(url, s)
来源:https://stackoverflow.com/questions/8244181/how-do-you-combine-url-fragments-in-java-the-same-way-browsers-do