Reputation: 653
Is there any way to intercept redirect requests in Android WebView? shouldInterceptRequest(WebView,WebResourceRequest) does not seem to get called!
I wanted to automatically catch token expiration in all web view requests by:
Item 2 is working fine, but item 3 is failing spectacularly since shouldInterceptRequest seems not to be called for redirects, which seems really wrong -- particularly since this is not documented and the WebResourceRequest API would lead one to believe that one can even check whether the request is a redirect.
I'd happily respond to 401's instead of using redirects -- but I see no way to retry the request "in situ" with an updated token, unless the request happens to be a top-level page request.
I suppose I could try the old "in page" redirect instead of a 302 to see if that works any better, but even if it does that is really a hack.
(Note that this is clearly a different issue than Android WebView, how to handle redirects in app instead of opening a browser -- as I already have a webview and am trying to intercept and manipulate redirection requests.)
Upvotes: 6
Views: 4921
Reputation: 4974
Example of interception on Kotlin:
webView.webViewClient = object: WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
if (url.startsWith("https://example.com")) {
// ...
return false
}
return super.shouldOverrideUrlLoading(view, url)
}
}
Upvotes: 0