17

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?

dimakura
  • 7,575
  • 17
  • 36
Idrizi.A
  • 9,819
  • 11
  • 47
  • 88
  • You did not show code that intercepts requests. Requests of whom? – greenapps Sep 10 '15 at 13:43
  • Requests of `WebView` of course. The above code intercept requests. I just didn't write `public WebResourceResponse shouldInte...` so the question would be more clean. – Idrizi.A Sep 10 '15 at 19:16
  • For me this code looks fine except if (url.lastIndexOf('.') > url.lastIndexOf('/')) This can set wrong mime type to response. If we, for example, assume that URL to PNG image is http://example.com/image1 your mime type will be wrong. I recommend you to replace that code with something like this http://stackoverflow.com/a/30101299/669159 to conclude what should be response mime. – mommcilo Sep 14 '15 at 15:33
  • Why do you need to intercept the requests? At least from your code it looks as you are just setting the UserAgent. Have you tried using setUserAgentString? http://developer.android.com/reference/android/webkit/WebSettings.html#setUserAgentString(java.lang.String) – Raanan Sep 21 '15 at 09:51
  • 1
    @Raanan I actually do some more stuff, like adding cookies for some domains and for some others not, which I didn't include in the question, because I am certain that the problem in the code is getting the content type of the requested URL. – Idrizi.A Sep 21 '15 at 15:51
  • Did you see this: http://stackoverflow.com/questions/5801993/quickest-way-to-get-content-type ? – Raanan Sep 21 '15 at 16:34

1 Answers1

13

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());
    }
}
Ilya Tretyakov
  • 6,848
  • 3
  • 28
  • 45
  • This code works, but it is a solution where some requests are intercepted and some other not. Still waiting for a better answer... – Idrizi.A Sep 18 '15 at 16:05