2017-08-24 92 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