Webview HTTPS handshake failed
Asked Answered
B

3

10

I'm trying to access a website from an Android application with WebView library, so I have:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        myWebView.setWebViewClient(
                new SSLTolerentWebViewClient()
        );
        myWebView.loadUrl("https://www.mywebsite.ro");
    }
}
class SSLTolerentWebViewClient extends WebViewClient {
    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        if (error.toString() == "piglet")
            handler.cancel();
        else
            handler.proceed(); // Ignore SSL certificate errors
    }
}

In debug I have:

D/OpenSSLLib: OpensslErr:Module:20(119:258); file:external/openssl/ssl/s23_clnt.c ;Line:714;Function:ssl23_get_server_hello

W/chromium: external/chromium/net/socket/ssl_client_socket_openssl.cc:171 [0209/105028:WARNING:ssl_client_socket_openssl.cc(171)] Unmapped error reason: 258

E/chromium: external/chromium/net/socket/ssl_client_socket_openssl.cc:792 [0209/105028:ERROR:ssl_client_socket_openssl.cc(792)] handshake failed; returned -1, SSL error code 1, net_error -2

D/chromium/tcp: LogConnectCompletion with error:-2

D/WebRequest: request error: -2

D/chromium: Unknown chromium error: -2

V/webview: setCertificate=null

V/webcore: setupViewport mRestoredScale=0.0 mViewScale=2.0 mTextWrapScale=2.0 data.mScale= 2.0

V/webcore: viewSizeChanged w=360; h=559; textwrapWidth=360; scale=2.0

D/skia: Flag is not 10

Beilul answered 9/2, 2018 at 9:2 Comment(2)
is this issue resolvedPother
Relevant answer here: stackoverflow.com/a/76442878Unsaddle
M
14

If you do not upload your app to the Play Store, you can just ignore SSL certificate errors:

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed(); // Ignore SSL certificate errors
}
Marte answered 15/2, 2018 at 3:33 Comment(2)
That's a bit of a silly solution. What would be the solution for if you do want to upload your app to the play store?Messenia
Please check this out if you are using user-added CAMarte
B
7

This problem is because the android versions under 4.4 does not support new versions TLS 1.1, TLS 1.2, which almost all sites use these versions.

Here is a list of android versions and support for TLS. enter image description here

Beilul answered 7/3, 2019 at 10:30 Comment(0)
D
0

dont try to process ssl error url in android webview otherwise android your app will be rejected from play store

don't try handler.proceed();

do this

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
          super.onReceivedSslError(view, handler, error);
          Log.e("onReceivedSslError", "-- " + error.getUrl() + " " + error.getCertificate() + " " + view.getUrl());
          showDialog(error.getUrl());
}

public void showDialog(String url) {
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
      alertDialog.setTitle("Error");
      alertDialog.setCancelable(false);
      alertDialog.setMessage("Getting SSL Error open in other browser");
      alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              openBrowser(WebViewActivity.this, url);
              dialog.dismiss();
          }
      });
      alertDialog.create().show();
  }




  public static void openBrowser(AppCompatActivity activity, String url) {
      Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
      if (activity == null) return;
      if (browserIntent.resolveActivity(activity.getPackageManager()) != null) {
          activity.startActivity(Intent.createChooser(browserIntent, "Choose browser"));
      } else {
          showToast(activity, "No application can handle this request.\"\n" +
                  "        + \" Please install a webbrowser");
      }
  }

Daladier answered 20/9, 2020 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.