WebView: How to find the base URL of the current page?
WebView has getUrl() and it has loadDataWithBaseUrl() but I can't find a method that returns the base URL (note: "base URL" is not the same as server/host path).
Is there a way to find the base URL of the current web page, even for pages that don't use the tag, but do use relative URLs in links contained in the page?
Note the complexity of the problem:
- getUrl() isn't always the base URL.
- Server/host path isn't always the base URL.
- Web pages don't always contain the
<base开发者_JAVA百科>
tag.
This may explain why there isn't a single WebView
method to provide the base URL...
You can do:
URL url = new URL(mWebView.getUrl());
With the URL
object, you can do get things like path, host, etc. Check out the docs for more info.
I was struggling with the same problem, but randomly found a solution. In method loadDataWithBaseURL
set the historyUrl
to your baseUrl
. Now when you requesting webView.getUrl()
it always returns your baseUrl
.
精彩评论