簡體   English   中英

WebViewClient :: onReceivedSslError不被調用

[英]WebViewClient::onReceivedSslError is not called

我已將我的WebViewClient實現附加到我的WebView。

appView.setWebViewClient(new AppViewClient());

調用了我的onReceivedSslErroronReceivedError實現,並帶有除Mixed Content錯誤之外的所有預期錯誤。

我對兩種方法的實現:

Log.i(TAG, "Error Cought");

就像我說的那樣,除了Mixed Content錯誤外,它們還會因各種錯誤而被調用。 我的請求被阻止,而沒有調用這些方法之一。

您使用的是哪個API版本? 默認情況下, MIX CONTENT允許在21之前。

您可以嘗試在網絡視圖中添加此設置:

webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);

對於低於21的API級別:

try {
    Method m = WebSettings.class.getMethod("setMixedContentMode", int.class);
    if ( m != null ) {

     m.invoke(webView.getSettings(), 1); //MIXED_CONTENT_NEVER_ALLOW

    }
}
catch (Exception ex) {

}

很高興知道您已經了解MixedContentMode :)我可以建議您嘗試2件事情。

  1. 使用控制台消息來識別此錯誤。 您可以進行字符串匹配以檢查MIX CONTENT錯誤。
myWebView.setWebChromeClient(new WebChromeClient() {
  public void onConsoleMessage(String message, int lineNumber, String sourceID) {
    Log.d("MyApplication", message + " -- From line "
                         + lineNumber + " of "
                         + sourceID);
  }
});
  1. 攔截請求並檢查網址。 檢查源代碼中的WebActivity實現。

     mWebView.setWebViewClient(new WebViewClient() { @Override public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { String url = request.getUrl().toString(); if (!URLUtil.isHttpsUrl(url)) { Logger.loge("Secure connection required, but insecure URL requested " + "explicitly, or as a part of the page."); return createNewSecurityErrorResponse(); } return super.shouldInterceptRequest(view, request); } }); private WebResourceResponse createNewSecurityErrorResponse() { WebResourceResponse response = new WebResourceResponse("text/plain", "UTF-8", null); response.setStatusCodeAndReasonPhrase(HTTP_FORBIDDEN, "Secure connection required"); return response; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM