简体   繁体   English

Android WebView-拦截URL加载

[英]Android WebView - Intercept URL loading

I'm trying to determine the successful payment event from the payment portal. 我正在尝试从付款门户确定成功的付款事件。 You might know this process: you send a 'callback URL' as one of other parameters to the payment website. 您可能知道此过程:您将“回调URL”作为其他参数之一发送到付款网站。 When the payment is made the website re-directs the browser to your 'callback URL'. 付款后,网站会将浏览器重定向到您的“回调URL”。

Since this is Android application I specify a custom url with a custom scheme ('myapp://order/123') as a 'callback URL' Then I use the following technique to intercept the re-direct to my 'callback URL' to do some custom action. 由于这是Android应用程序,因此我将使用自定义方案('myapp:// order / 123')指定一个自定义网址作为“回调URL”,然后使用以下技术截取重定向至“回调URL”的网址做一些自定义动作。

mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            LOG.info("Inside shouldOverrideUrlLoading(), url: {}", url);

            if (url.startsWith("myapp://")) {
                onPaymentPerformed();
                return true;
            } else {
                return false;
            }
        }
}

It worked for many months but recently it started failing. 它工作了几个月,但最近却开始失败。 I don't know why but probably due to device updates. 我不知道为什么,但可能是由于设备更新。 This method has stopped being called for 'myapp://' url. 此方法已停止调用“ myapp://” URL。 I've checked the logs and found the following message 我检查了日志,发现以下消息

I/chromium: [INFO:CONSOLE(2174)] "Mixed Content: The page at 'https://www.liqpay.com/en/checkout/success/xxxx' was loaded over a secure connection, but contains a form which targets an insecure endpoint 'myapp://order/7'. This endpoint should be made available over a secure connection.", source: https://static.liqpay.com/checkout/160922113118/js/index.js (2174)

Then I tried to change 'myapp://order/123' to ' https://order/123 ' but the method shouldOverrideUrlLoading() is also not called for this url, instead I see a standard error message in the webview: 然后我尝试将'myapp:// order / 123'更改为' https:// order / 123 ',但此网址也未调用方法shouldOverrideUrlLoading(),而是在Webview中看到了标准错误消息:

The webpage at https://order/123 could not be loaded because: net::ERR_NAME_NOT_RESOLVED

I found nothing similar to this on Internet, please help 我在互联网上找不到与此类似的东西,请帮忙

You could subclass the WebClient and override the onReceiveSslError method. 您可以子类化WebClient并重写onReceiveSslError方法。

 private class SSLTolerentWebViewClient extends WebViewClient {
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed(); // proceed ignoring ssl error. 
    }
}

See if that works. 看看是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM