How can I make a WebView request the CSS and javascript links when I load HTML externally into the WebView?
This question is similar to:
https://stackoverflow.com/questions/34608714/css-and-js-files-not-loading-in-android-webview
but we are loading everything from a server. In order to support SSL Pinning we override the
loadUrl(String url)
method (and will override the others) and handle the request with an HttpClient.execute call. When we receive the data back from the network, then we push it into the webview with
WebView.loadData(event.getContent(), event.getMimeType(), event.getEncoding());
How should we implement this design to retrieve data externally and then load it into the webview? The approach above shows the HTML fine but the CSS files are not loaded.
You don't. Use the loadDataWithBaseURL. Then the WebView will fire requests to load the css/js/png. The tricky part is you can override WebView.loadUrl for the initial request but then override WebViewClient.shouldInterceptRequest for each of the css/js/png requests. In there you can fire your network request and return the response as a stream (WebResourceResponse).
Related
Is there any way to intercept redirect requests in Android WebView? shouldInterceptRequest(WebView,WebResourceRequest) does not seem to get called!
I wanted to automatically catch token expiration in all web view requests by:
Issuing a redirect rather than a 401 from the server
Using shouldOverrideUrlLoading() to invoke AppAuth where a login is necessary
Using shouldInterceptRequest to bounce back to the original URL with a refreshed token where a login is not necessary
Item 2 is working fine, but item 3 is failing spectacularly since shouldInterceptRequest seems not to be called for redirects, which seems really wrong -- particularly since this is not documented and the WebResourceRequest API would lead one to believe that one can even check whether the request is a redirect.
I'd happily respond to 401's instead of using redirects -- but I see no way to retry the request "in situ" with an updated token, unless the request happens to be a top-level page request.
I suppose I could try the old "in page" redirect instead of a 302 to see if that works any better, but even if it does that is really a hack.
(Note that this is clearly a different issue than Android WebView, how to handle redirects in app instead of opening a browser -- as I already have a webview and am trying to intercept and manipulate redirection requests.)
Example of interception on Kotlin:
webView.webViewClient = object: WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
if (url.startsWith("https://example.com")) {
// ...
return false
}
return super.shouldOverrideUrlLoading(view, url)
}
}
I am trying to develop a custom webview app where in only google safe browsing sites will be opened in webview.
For that I need to send url requested in webview to google first, wait for google safe browsing api response and if safe then load the url in webview.
For that I am intercepting url in shouldOverrideUrlLoading, and sending an http request from there. But since shouldOverrideUrlLoading is called on UI thread, I get networkonmainthread exception. I can send request in async task but it wont stop webview from loading the url.
Is there any way by which I can block shouldOverrideUrlLoading without getting ANR or networkonmainthread exception?
I did not found an acceptable solution for rewriting all urls referenced in an WebView of an Android-App.
I mean all urls!
Not just those from HTTP-GET Methods. I also need to rewrite urls with HTTP-POST, PUT, DELETE, HEAD methods and urls inside Javascript(with all http methods).
Why I want to do this? I want to make an app, accessing intranet content from outside. Let's say intranet is located on https://myintranet.com
Let's say I have web resources https://myintranet.com/resources/whatever.html. If I am logged in my desktop-machine in the firm I can access the resource directly.
Now we want to go mobile. Therefore we do have a proxy, which supports https and basic authentication. We now accessing a resource with https://myproxy.com/myInternalMapperToIntranet/resources/whatever.html
What did I try:
I tried to set the Web view a WebViewClientoverriding
public WebResourceResponse shouldInterceptRequest (WebView view, String url)
look here Unfortunately this only works for HTTP-GET requests but not for HTTP POST requests.
And a WebViewClient overriding
public boolean shouldOverrideUrlLoading (WebView view, String url)
same thing here, HTTP Posts mapped to a send Button or JavaScript do not provoking a call to one of those methods.
In my case the most easiest way would be, to get the HttpClient of the WebView, and intercepting all requests this urls wants to make and rewriting the url to the one with the Proxy-content.
I simply could define a regex and replace the Url. But it seems, this is not possible in Android. A Collegue did this on a iOS-App, but on Android it seems currently not possible. I found a possible solution #API 21 with:
public WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request)
the WebResourceRequests encapsulates the Http Method, so it smells for the possibility to intercept all httpMethods. Unfortunately I have to support API 17, and may not use this API.
I cant believe that this technical requirement cant be realized with standard android components. Please prove me the opposite!
Does anybody knows alternatives?
Doing the whole rewriting in the back-end for many simultaneous users is currently no alternative. It has to be done client-side on the App due performance issues.
TIA Luke
is there a way to get request and response objects from a webview?
for requests made from some webpage running in my webview, i want to intercept the full http request object(the headers, http method used, http body etc) and divert and send across that request into another channel.
For responses received from the webview, i want to do the same and get the object and its properties.
So far i have looked at the webviewClient android class which allows you to intercept url links executed by a webpage and intercept the resources it loads.
However, what i want to intercept, is any actual http requests the webpage makes. is this possible in Android webview?
thanks
That is not directly possible. You are welcome to write an HTTP proxy, then attempt to get WebView to work with that (e.g., see if it supports the http.proxyHost and http.proxyPort system properties).
I'm developing a browser-like application in android using WebView.
While loading the URL, I want to display a specific page/message/toast whenever server returns 404 response. I tried using shouldOverrideUrlLoading, but it can filter URLs based on URL string only. Is there any good way to get the server's response code within WebViewClient or WebChromeClient (without loading the URL twice)?
override the method onReceivedError of WebViewclient