title: The best way to intercept a WebView request in Android
body: <p>I am using a <code>WebView</code> in my app in which I must intercept requests. I am currently using the follwing code to do it.</p>
  
  <pre class="lang-java prettyprint-override"><code>public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
      HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
      conn.setRequestProperty("User-Agent", userAgent);
  
      String mime;
      if (url.lastIndexOf('.') &gt; 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());
  }
  </code></pre>
  
  <p>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 <code>shouldInterceptRequest</code>.</p>
  
  <p>Is there any better way that the one I am currently using to intercept requests?</p>