So I'm trying to make a very basic web browser that accomplishes very specific tasks. However, I need to get the URL from relative URLs (such as in tags. I can get both URLs, but I'm not sure how to approach relative URLs.
I am using Java 6 for compatibility with older systems (a lot older)
Basically, I have the URL "http://example.com/directory/page.html", then I have an tag with the href= "newpage.html". I want to be able to get the URL "http://example.com/directory/newpage.html".
Moreover, if its href= "../newpage.html", I want to get "http://example.com/newpage.html",
and if its href="http://example.org/dir/anotherpage.html", I want to get the URL "http://example.org/dir/anotherpage.html".
Is there any good, clean way of doing this?
You can simply use the uri.resolve()
method.
First create a URI
from the base URL you loaded in Browser:
URI uri = new URI("http://example.com/directory/page.html");
URI newpage = uri.resolve("newpage.html");
System.out.println(newpage);
This will print:
The result for uri.resolve("../newpage.html")
is :
The result for uri.resolve("http://example.org/dir/anotherpage.html")
is:
Of course you could check for an http
prefix before and return the absolute URL instead of using uri.resolve()
.
Even the usage of anchors, like #myanchor
is possible. The result of uri.resolve("#myanchor")
is: