Ignore ssl certificate requests in webview

2020-05-09 17:23发布

问题:

I make small app which involve aspx pages , I try open it throw my webview I got blank screen .

I try open it with chrome browser it give me certificate needed

my code is very simple I add some documentation to explain what is going on

@SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        Bundle bundle = getIntent().getExtras();
        if(bundle != null){
            urlSite = bundle.getString("passed_url");
        }
     _toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(_toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    _toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
    webView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = webView.getSettings();

    // enable javascript
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);


    webView.setWebChromeClient(new WebChromeClient());
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            new ParseURl().execute(new String[]{urlSite});
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
    if(urlSite != null){
        webView.clearCache(true);
        webView.loadUrl(urlSite);
       // webView.loadUrl("file:///android_asset/af.html");
    }
}

回答1:

After some researched I found answer , just overide onReceivedSslError method and pass it

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