2017-08-24 132 views
1

的WebView HTTPS URL結果是工作的罰款與HTTP請求和還HTTPS其中衆所周知的受信任的站點像https://www.online.citibank.co.in/ 但我嘗試用CA從第三方發出訪問專用網站,它給黑屏。證書通過SD卡安裝到手機上,並列在可信證書列表下。的Android的WebView負荷黑屏

當我向TrustManager添加證書後,使用HttpsURLConnection嘗試相同的URL時,它工作正常(能夠獲取內容)。

以下是WebView和HttpsURLConnection的代碼片段。

HttpsURLConnection的:這下面的代碼工作正常,並能獲得來自URL的內容(我不能夠共享URL,因爲它不是來自外界的訪問)

try 
{ 
    SSLContext context = null; 

    CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
    InputStream caInput = getResources().openRawResource(R.raw.mi_net); 
    Certificate ca; 
    try { 
     ca = cf.generateCertificate(caInput); 
    } finally { 
     caInput.close(); 
    } 

    // Create a KeyStore containing our trusted CAs 
    String keyStoreType = KeyStore.getDefaultType(); 
    KeyStore keyStore = KeyStore.getInstance(keyStoreType); 
    keyStore.load(null, null); 
    keyStore.setCertificateEntry("ca", ca); 

    // Create a TrustManager that trusts the CAs in our KeyStore 
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); 
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); 
    tmf.init(keyStore); 

    // Create an SSLContext that uses our TrustManager 
    context = SSLContext.getInstance("TLS"); 
    context.init(null, tmf.getTrustManagers(), null); 

    url = new URL(urlStr); 
    HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); 
    con.setSSLSocketFactory(context.getSocketFactory()); 
    con.setInstanceFollowRedirects(true); 

    con.setDoOutput(false); 
    con.setConnectTimeout(1000); 
    String responseMsg = con.getResponseMessage(); 
    response = con.getResponseCode(); 
    is = con.getInputStream(); 
} 

的WebView :不工作,調用的回調onReceivedSslError

{ 
    WebSettings viewSettings = webView.getSettings(); 
    viewSettings.setJavaScriptEnabled(true); 
    viewSettings.setAllowContentAccess(true); 
    viewSettings.setBuiltInZoomControls(false); 
    webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); 
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
    webView.loadUrl(sameURL); 

    webView.setWebViewClient(new WebViewClient() { 
     @Override 
     public void onPageStarted(final WebView view, final String url, Bitmap favicon) { 
      Log.d("ann", "onPageStarted"); 

     } 

     @Override 
     public void onPageFinished(final WebView view, String url) { 
      Log.d("ann", "inside onPageFinished"); 
     } 

     @Override 
     public void onReceivedError(WebView view, int errorCode, 
            String description, String failingUrl) { 

      if (!failingUrl.startsWith("mailto:")) { 
       webView.loadUrl("file:///android_asset/html/error.html"); 
      } 

     } 

     @Override 
     public void onReceivedSslError(WebView view, SslErrorHandler handler, 
             SslError error) { 
      super.onReceivedSslError(view, handler, error); 
      Log.d("ann","SSL error"); 

      handler.proceed(); 
     } 

    });} 
} 

請幫我的建議。 WebViewClient異常是I/X509Util:無法驗證證書鏈,錯誤:java.security.cert.CertPathValidatorException:未找到證書路徑的信任錨。

回答

0

對於HttpsUrlConnection,您將從文件創建證書並在運行時對其進行設置。

Webview必須使用已經在系統中的任何東西。

下面是一種變通方法類似的問題提出:

Check in the onReceivedSslError() method of a WebViewClient if a certificate is signed from a specific self-signed CA

+0

@Override 公共無效onReceivedSslError(web視圖視圖,SslErrorHandler處理程序, SslError錯誤){// super.onReceivedSslError(視圖,處理程序,錯誤); Log.d(「ann」,「SSL錯誤」); handler.proceed(); }評論超級功能後,事情開始爲我工作。 –

+0

請注意,此替代方法可以繞過證書檢查,因此您應該處理邏輯,以便再次檢查包含的證書作爲文件。否則,就像不使用https。 – Juan