0

I have implemented the URL whitelisting in the Android web view by using shouldOverrideUrlLoading.

 override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
          if (isAllowedUrl(request?.url)) {
            return super.shouldOverrideUrlLoading(view, request)
          }else{
           // request external browser
           return true
          }
 }

This could intercept all the URL redirects. Is there any way to intercept background API calls made by the website? Our website making some analytics calls. I want to conditionally allow/block these requests. Is it possible to intercept those requests in the Android web view?

The shouldInterceptRequest can intercept all requests, but I couldn't find a way to block the request.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Suneesh Ambatt
  • 1,347
  • 1
  • 11
  • 41
  • Does this answer your question? [Android: Intercept AJAX call from WebView](https://stackoverflow.com/questions/3941969/android-intercept-ajax-call-from-webview) – Ferdau Jun 22 '21 at 09:06
  • @Ferdau Partially, Is there any way to cancel the request from shouldIntercept request callback? – Suneesh Ambatt Jun 22 '21 at 10:34
  • On https://developer.android.com/reference/android/webkit/WebViewClient#shouldInterceptRequest(android.webkit.WebView,%20java.lang.String) you can see: "Notify the host application of a resource request and allow the application to return the data. If the return value is null, the WebView will continue to load the resource as usual. Otherwise, the return response and data will be used." Just return something for requests you want to block. – Quickern Jun 22 '21 at 11:40

0 Answers0