How to solve problem when showing URL that has a security certificate

1

When I show a url that has a security certificate (https: //), the view goes blank, but those that do not have a security certificate load without problem (http: //).

Next the code that shows the page view with security certificate, I use a webView container.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_noticias, container, false);

    WebView view=(WebView) v.findViewById(R.id.wv_noticias);
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new WebViewClient());// Agregamos un WebViewCliente, esto permite que se sigan ejecutando los links dentro de este WebView
    //view.loadUrl(url);
    /****************************************/
    if (isOnline(getActivity())) {
    String url="https://www.uniagustiniana.edu.co/Noticias/";

        view.loadUrl(url);
        //Toast.makeText(getActivity(),"SI hay conexión!",Toast.LENGTH_SHORT).show();
    } else {
        view.loadUrl("file:///android_asset/html/pagina_error.html");
        Toast.makeText(getActivity(),"NO hay 
    conexión!",Toast.LENGTH_SHORT).show();
    }
    /****************************************/

    return v;
}
    
asked by Ivan Alfredo 14.08.2018 в 01:17
source

2 answers

1

Using the WebClient you should write about the method onReceivedSslError () .

  

onReceivedSslError () Notifies the host application that it   An SSL error occurred while loading a resource. The host application must   call handler.cancel() or handler.proceed() .

Example:

WebView view=(WebView) v.findViewById(R.id.wv_noticias);
view.getSettings().setJavaScriptEnabled(true);

webview.setWebViewClient(new WebViewClient() {

            ...
            ...
            ...

            @Override
            public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
                String message = "SSL Certificate error.";
                switch (error.getPrimaryError()) {
                    case SslError.SSL_UNTRUSTED:
                        message = "The certificate authority is not trusted.";
                        break;
                    case SslError.SSL_EXPIRED:
                        message = "The certificate has expired.";
                        break;
                    case SslError.SSL_IDMISMATCH:
                        message = "The certificate Hostname mismatch.";
                        break;
                    case SslError.SSL_NOTYETVALID:
                        message = "The certificate is not yet valid.";
                        break;
                }
                message += "\"SSL Certificate Error\" Do you want to continue anyway?.. YES";

                handler.proceed();

                Log.e(TAG, onReceivedSslError: " + message);
            }

        });

The common thing is to cancel the load but in this case it simply indicates that I continued with handler.proceed()

Update:

In the case of uploading an application to the Google Play Store, care must be taken as it will probably not be accepted, the reason is that if an SSL error occurs the page should not be loaded, otherwise it should be canceled, due to issues of security.

link

For this you must implement a dialog which indicates to the user if you want to proceed or cancel the upload, with this your application can be uploaded to Google Play Store without problem.

  @Override
    public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(CustomWebView.this);
        String message = "";
        switch (error.getPrimaryError()) {
            case SslError.SSL_UNTRUSTED:
                message = "The certificate authority is not trusted.";
                break;
            case SslError.SSL_EXPIRED:
                message = "The certificate has expired.";
                break;
            case SslError.SSL_IDMISMATCH:
                message = "The certificate Hostname mismatch.";
                break;
            case SslError.SSL_NOTYETVALID:
                message = "The certificate is not yet valid.";
                break;
            default:
                break;
        }
        message += "\"SSL Certificate Error\" Deseas continuar?";

        builder.setTitle("SSL Certificate Error");
        builder.setMessage(message);
        builder.setPositiveButton("continuar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                handler.proceed();
            }
        });
        builder.setNegativeButton("cancelar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                handler.cancel();
            }
        });
        builder.create().show();
      }
    
answered by 14.08.2018 / 17:57
source
1

Good day Ivan you just have to add a WebViewClient you can add it in the following way:

view.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed(); // Ignore SSL certificate errors
        }
    }); 

I hope you find it useful.

    
answered by 14.08.2018 в 17:01