title: Handling certificate errors in Android Webview and clearing the certificate peferences
body: <p>I am trying to find a proper way to handle SSL certificate errors in the Android Webview.
  My goal is to provide a way to load pages with SSL certificate errors, but let the user choose to load the page after warning him about security <strong>any time he tries to load a URL with certificate errors</strong>.</p>
  
  <p>The closest solutions I found in threads suggest overriding the WebViewClient as following:</p>
  
  <pre><code>webView.setWebViewClient(new WebViewClient() {
      @Override
      public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError error) {
          handler.proceed();
      }
  });
  </code></pre>
  
  <p>However this basically disables SSL in the WebView without the user consent.</p>
  
  <p>For reference here are the threads where I found that solution:</p>
  
  <p><a href="https://stackoverflow.com/questions/3630445/android-webview-ssl-security-warning">Android WebView SSL &#39;Security Warning&#39;</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5977977/does-the-web-view-on-android-support-ssl">Does the Web View on Android support SSL?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7416096/android-webview-not-loading-https-url">Android WebView not loading an HTTPS URL</a></p>
  
  <p><a href="https://stackoverflow.com/questions/15588851/android-webview-with-client-certificate">android webview with client certificate</a></p>
  
  <p><a href="https://stackoverflow.com/questions/23124019/web-view-shows-blank-white-page-after-loading-url-when-using-wifi-in-android">Web view shows blank/white page after loading URL when using WIFI in Android</a></p>
  
  <p><a href="https://stackoverflow.com/questions/23348498/unable-to-load-a-specific-webpage-on-android-webview">Unable to load a specific webpage on Android webview</a></p>
  
  <p><a href="https://stackoverflow.com/questions/24004502/webview-displays-a-blank-view-for-certain-links">WebView displays a blank view for certain links</a></p>
  
  <p><a href="https://stackoverflow.com/questions/28626433/android-webview-blocks-redirect-from-https-to-http">Android WebView blocks redirect from https to http</a></p>
  
  <p><a href="https://stackoverflow.com/questions/34257257/ignore-ssl-certificate-requests-in-webview">Ignore ssl certificate requests in webview</a></p>
  
  <p>I went ahead and implemented a slightly different version which prompts the user:</p>
  
  <pre><code>webView.setWebViewClient(new WebViewClient() {
      @Override
      public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError error) {
          //Showing a first confirmation dialog
          AndroidUtils.showYesNoDialog(
              //First confirmation message
              "WARNING - THIS PAGE IS NOT SECURE! Are you sure you want to continue loading it?",
              //First confirmation "YES" option runnable
              new Runnable() {
                  @Override
                  public void run() {
                      //Showing a second confirmation dialog
                      AndroidUtils.showYesNoDialogWithResId(
                          //Second confirmation message
                          "You chose to load an unsecure page, are you sure you want to do that?",
                          //Second confirmation "YES" option runnable
                          new Runnable() {
                              @Override
                              public void run() {
                                  //Disregard the error and proceed with the bad certificate anyways
                                  handler.proceed();
                              }
                          },
                          //Second confirmation "NO" option runnable
                          new Runnable() {
                              @Override
                              public void run() {
                                  //Cancel loading the page with that certificate error
                                  handler.cancel();
                              }
                          }
                      );
                  }
              },
              //First confirmation "NO" option runnable
              new Runnable() {
                  @Override
                  public void run() {
                      //Cancel loading the page with that certificate error
                      handler.cancel();
                  }
              });
      }
  });
  </code></pre>
  
  <p>This implementation asks the user twice if he wants to load the page, if he says yes twice, then the error is disregarded and the page loads, otherwise the page loading is canceled.</p>
  
  <p>The first time a URL with certificate error loads, <code>WebViewClient.onReceivedSslError</code> is called, however if the user proceeds with the certificate error and <code>SslErrorHandler.proceed()</code> is called, the following times the same URL loads, <code>WebViewClient.onReceivedSslError</code> is never called again: only killing the app resets this behavior.</p>
  
  <p>I would want <code>WebViewClient.onReceivedSslError</code> to be called systematically when a URL with a certificate error loads, not just the first time. I tried calling those methods without success:</p>
  
  <pre><code>/** JAVADOC QUOTE: Clears the SSL preferences table stored in response to proceeding with SSL certificate errors.*/
  webView.clearSslPreferences();
  //Those other methods I tried out of despair just in case
  webView.clearFormData();
  webView.clearCache(true);
  webView.clearHistory();
  webView.clearMatches();
  </code></pre>
  
  <p><strong>Does anybody know how to make the WebView call <code>WebViewClient.onReceivedSslError</code> more than once for the same URL, after <code>SslErrorHandler.proceed()</code> has been called?</strong></p>