Skip to content
Görkem KaraLead Mobile Developer

Android Webview shouldInterceptRequest: Advanced Features

Android Development3 min read

Android WebView control using shouldInterceptRequest for blocking ads and optimizing data.

shouldInterceptRequest is a method of WebViewClient that Android calls for every resource request your WebView makes, such as images, scripts and styles. Override it in your own client and return a WebResourceResponse to block the request, change it or answer it from local data; return null and the WebView loads the resource normally. Below you will find working examples for redirecting and modifying requests, ad blocking, data compression, local caching and adding authentication headers.

Diagram of the key steps in Android Webview shouldInterceptRequest: Advanced Features: What is shouldInterceptRequest, and Why Should You Care?, Redirecting and Modifying Requests, Ad Blocking in WebView

What is shouldInterceptRequest, and Why Should You Care?

At its core, shouldInterceptRequest is like a gatekeeper for every network request your WebView makes. When a page inside your WebView tries to fetch any resource (like images, scripts, or styles), shouldInterceptRequest lets you intercept the request, inspect it, and decide whether to allow, modify, or block it entirely.

Here’s how to set up a basic shouldInterceptRequest in a CustomWebViewClient:

class CustomWebViewClient : WebViewClient() {
    override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
        // Your custom logic goes here
        return super.shouldInterceptRequest(view, request)
    }
}

Redirecting and Modifying Requests

Imagine you want to load a cached version of a resource or redirect a URL to save bandwidth. With shouldInterceptRequest, you can easily swap out the requested resource for a different one.

override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
    val url = request?.url ?: return null
    // Serve the logo from the app's assets instead of downloading it again
    if (url.host == "example.com" && url.path == "/images/logo.png") {
        val context = view?.context ?: return null
        return WebResourceResponse(
            "image/png", null,
            context.assets.open("web/logo.png")
        )
    }
    return super.shouldInterceptRequest(view, request)
}

Tip: Don’t go overboard with redirects. Redirecting multiple requests can slow down your WebView’s performance, especially on low-end devices.

Ad Blocking in WebView

Let’s face it, ads are everywhere, and sometimes they can overwhelm the user experience. By using shouldInterceptRequest, you can block those pesky ads based on their URLs.

val blockedUrls = listOf("adserver.com", "trackersite.com")
override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
    val url = request?.url.toString()
    if (blockedUrls.any { url.contains(it) }) {
        return WebResourceResponse("text/plain", "utf-8", ByteArrayInputStream("".toByteArray()))
    }
    return super.shouldInterceptRequest(view, request)
}

In this example, we use a simple list of blocked URLs. If the requested URL matches any in the list, we return an empty response, effectively blocking the ad.

Data Compression: Save Bandwidth

If your app loads lots of data-heavy resources, you can reduce bandwidth by compressing the data before sending it. Here’s how you can set up gzip compression:

override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
    val connection = URL(request?.url.toString()).openConnection()
    connection.setRequestProperty("Accept-Encoding", "gzip")

    val inputStream = if (connection.getHeaderField("Content-Encoding") == "gzip") {
        GZIPInputStream(connection.getInputStream())
    } else {
        connection.getInputStream()
    }

    return WebResourceResponse(
        "text/html", "utf-8", inputStream
    )
}

Why Compress?

Compression can save bandwidth and reduce loading times, which is crucial for users with limited data plans or in areas with slow connections.

Caching Resources Locally

Let’s say you want to load some resources only once and cache them for later use. This approach can significantly improve performance, especially if your app uses the same resources repeatedly.

override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
    val url = request?.url.toString()
    val cachedResponse = getFromCache(url)

    return if (cachedResponse != null) {
        WebResourceResponse("text/html", "utf-8", ByteArrayInputStream(cachedResponse))
    } else {
        super.shouldInterceptRequest(view, request)
    }
}

private fun getFromCache(url: String): ByteArray? {
    // Implement caching logic here
    return null
}

In this example, getFromCache attempts to fetch the requested resource from local storage. If it finds it, it returns the cached response; otherwise, it allows the request to proceed normally.

Adding Authentication Headers

For resources that require authentication, like APIs, you can include custom headers with your requests. Here’s how:

override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
    val connection = URL(request?.url.toString()).openConnection() as HttpURLConnection
    connection.setRequestProperty("Authorization", "Bearer YOUR_TOKEN")

    val inputStream = connection.inputStream
    return WebResourceResponse(
        "text/html", "utf-8", inputStream
    )
}

In this example, we’re adding an Authorization header to each request. This is particularly useful when accessing secure resources.


Using shouldInterceptRequest with Android’s WebView unlocks a world of possibilities for controlling, optimizing, and securing your in-app web browsing experience. Whether you’re blocking ads, compressing data, caching resources, or adding authentication, these techniques can dramatically improve both performance and user experience. Play around with these tips, and don’t forget to keep the user experience in mind while customizing your WebView. If you also need the session cookies from the same WebView, see how to retrieve cookies with Android WebView in Kotlin. And if you are working on an app that needs request interception, caching or authenticated content inside a WebView, you can reach me through the contact page for mobile app development or consulting.

Last updated: Written by: Görkem KARA

#android webview#webview

What did you think of this post?

Comments

No comments yet. Be the first to write one.

Leave a comment

Comments appear after the site owner approves them.

Your comment is published without your name; your email is used only for this comment and never published. Privacy notice

Security check Runs automatically when you send