Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The best way to intercept a WebView request in Android

I am using a WebView in my app in which I must intercept requests. I am currently using the follwing code to do it.

public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setRequestProperty("User-Agent", userAgent);

    String mime;
    if (url.lastIndexOf('.') > url.lastIndexOf('/')) {
        String ext = url.substring(url.lastIndexOf('.') + 1);
        mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
    } else {
       mime = "text/html";
    }
    return new WebResourceResponse(mime, "UTF-8", conn.getInputStream());
}

Above code works fine in most cases, but no all. For example when I try to login to Outlook, it just shows that my email or password is incorrect, I have also seen other cases in which requests get broken, but everything works fine if I remove shouldInterceptRequest.

Is there any better way that the one I am currently using to intercept requests?

like image 401
Enve Avatar asked Sep 10 '15 13:09

Enve


People also ask

How can I improve my WebView performance?

Enhance webView performance - disable the WebView cache to make WebView much faster. Supercharging the Android WebView - cache critical assets that drastically reduce the load time.

How do you communicate between WebView and native Android?

2.1 To receive data from webview ,we can create an interface, which will enable webview to connect the native layer and pass data. From native layer, create a class and replicate the following. While configuring web view, we need to set JavaScript interface as above JSBridge class.

How do you override a WebView?

If you want to override certain methods, you have to create a custom WebView class which extends WebView . Also, when you are inflating the WebView , make sure you are casting it to the correct type which is CustomWebView . CustomWebView webView = (CustomWebView) findViewById(R. id.

What is shouldOverrideUrlLoading?

shouldOverrideUrlLoading is called when a new page is about to be opened whereas shouldInterceptRequest is called each time a resource is loaded like a css file, a js file etc.


Video Answer


1 Answers

There are two issues with you code

  1. Incorrect extension detection

For example, when the code try to get resource extension for this URL:

https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=12&ct=1442476202&rver=6.4.6456.0&wp=MBI_SSL_SHARED&wreply=https:%2F%2Fmail.live.com%2Fdefault.aspx%3Frru%3Dinbox&lc=1033&id=64855&mkt=en-us&cbcxt=mai

It will return aspx%3Frru%3Dinbox&lc=1033&id=64855&mkt=en-us&cbcxt=mai which is wrong. There is special method for getting extension from the URL: getFileExtensionFromUrl()

  1. According to documentation method MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext) may return null. In this case your code set wrong mime type for the page.

Here is the method code that take into account both these issues

@Override
public WebResourceResponse shouldInterceptRequest(WebView view,
    String url) {
    String ext = MimeTypeMap.getFileExtensionFromUrl(url);
    String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
    if (mime == null) {
        return super.shouldInterceptRequest(view, url);
    } else {
        HttpURLConnection conn = (HttpURLConnection) new URL(
                                                 url).openConnection();
        conn.setRequestProperty("User-Agent", userAgent);
        return new WebResourceResponse(mime, "UTF-8",
                                                 conn.getInputStream());
    }
}
like image 155
Ilya Tretyakov Avatar answered Sep 17 '22 15:09

Ilya Tretyakov