Error redirection in WebView - android

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

Related

How to trigger css and js load with Android WebView loadData

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).

Blocking shouldOverrideUrlLoading in android webview

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?

Android-the method loadUrl() in webView sends POST or GET request?

i'm working with webview recently and i found 2 methods to load an URL in webView.
For postUrl() the doc said clearly it sends a POST request, but it didn't say anything about loadUrl().
can anybody tell me anything about it?

Android get request and response objects from webview

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).

Silently authentication from webview

Here's what I want to have figured out : I'm using a webview to access a feature of a site that requires authentication. Therefore whenever that particular link is loaded the login page is displayed . Is is possible to somehow authenticate silently so that the particular feature is displayed directly ? Perhaps by using the "webView.setHttpAuthUsernamePassword" (which does not seem to work for me, or I don't do it right) or by making some POST or GET before I load the page , or other possibility ?
On logging in the server is simply supposed to send me a cookie.
That method only works for HTTP Basic Auth, not for form-based login.
Your best bet would be to use the onLoadResource method of the WebViewClient to detect that the page is about to load, then override the content of the WebView using loadData with a chunk of HTML/js that posts to the login form with the proper parameters, then detect the successful cookie return and forward on to the original url.
Depending on the site it may be messy: you may have to set the proper referer headers or CSRF tokens and other things of that nature.
Good luck.
See, you could do the task this way, I am not sure if this is what you want:
#Override
public void onReceivedHttpAuthRequest(
WebView view,
HttpAuthHandler handler,
String host,
String realm) {
String currentUrl = yourWebView.getUrl();
if(currentUrl.equals("www.yourwebsite.com")){
handler.proceed("username","password");
}
}

Categories

Resources