Disable Android WebView/WebViewClient Initiated favicon.ico Request

旧街凉风 提交于 2019-12-01 17:29:50
ventura8

for me the complete solution was:

   @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

        if(url.toLowerCase().contains("/favicon.ico")) {
            try {
                return new WebResourceResponse("image/png", null, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    @SuppressLint("NewApi")
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {

        if(!request.isForMainFrame() && request.getUrl().getPath().endsWith("/favicon.ico")) {
            try {
                return new WebResourceResponse("image/png", null, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return null;
    }

I achieved this by a little hack. First, I've created a fake 1x1 icon file and saved it to the assets folder. Then I overrode WebViewClient's shouldInterceptRequest() method, where I check the URL whether it is the request for favicon file and in that case return WebResourceResponse with InputStream which contains our fake icon:

    @Override
    @CallSuper
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        if(!request.isForMainFrame() && request.getUrl().getPath().equals("/favicon.ico")) {
            try {
                return new WebResourceResponse("image/png", null, new BufferedInputStream(view.getContext().getAssets().open("empty_favicon.ico")));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

Note that the InputStream must not be closed in our code, because it is subsequently used by the WebView to read the icon. The WebviewClient must be set to the WebView via its setter:

mWebView.setWebViewClient(subclassedWebViewClient);

This is an answer for Kotlin

override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
    return if (request?.url?.lastPathSegment == "favicon.ico") {
        WebResourceResponse("image/png", null, null)
    } else {
        super.shouldInterceptRequest(view, request)
    }
}

By default, return type is not nullable WebResourceResponse when Android Studio generates code, but It does not work so change return type to nullable WebResourceResponse.

There is a method for WebView class named getFavicon(). I think that method is called by WebView to retrieve the favicon from server by issuing request. So you can try extending the WebView class and override the getFavicon() method to do nothing. I haven't tried it myself but this might work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!