title: Android Intercept a WebView request properly
body: <p>My current code for intercepting a request in webview is</p>
  
  <pre><code> @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());
      }
  }
  </code></pre>
  
  <p>I got this code from
  <a href="https://stackoverflow.com/questions/32503258/the-best-way-to-intercept-a-webview-request-in-android">The best way to intercept a WebView request in Android</a>.</p>
  
  <p>However, whenever I try to perform authentication, let's say I am loading facebook in my webview.</p>
  
  <pre><code>mWebView.loadUrl("https://www.facebook.com/");
  </code></pre>
  
  <p>Nothing is happening, what I noticed is that, the request headers are incomplete and also the response. Also, there are no cookies in the Sources. (I saw this when I remotely debugged the webview through Chrome).</p>
  
  <p>Please correct me if I'm wrong, but I think that the incomplete headers and missing cookies is what causing the login request to fail.</p>
  
  <p>Is there a way where I can modify the request and set its headers? Also for the response, should I do it too? And finally, how will I be able to have the cookies.</p>