Search code examples
javaexceptionhttpsjava-web-starthttpsurlconnection

HttpsURLConnection show Security Warning Dialog rather than catch Exception


I make an HttpsURLConnection as below:

        try
        {
            URL url = new URL( host );
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.connect();
            logger.debug( httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK );
        }
        catch ( Exception ex )
        {
            logger.error( ex.getMessage() );
        }

The host I tested doesn't have a valid certificate, so when I tested within Eclipse, it catch SSLHandshakeException which its fine.

However, when I deployed as WebStart, it doesn't go to catch clause but showing me the warning dialog instead:

enter image description here

If user click Continue, it passes the connect() successfully.

Is there anyway that I can catch the exception instead allowing User to click Continue from this dialog ?


Solution

  • To disable the dialog use this:

    connection.setAllowUserInteraction(false);

    Now you should get the exception immediately if the JVM is not happy with the certificate.