Java Https - HttpsURLConnection - Unsupported SSLv2Hello

4

When making an https request I have the following exception:

  

javax.net.ssl.SSLException: Unsupported record version SSLv2Hello

public static void main(String[] args) throws Exception {
        URL url = new URL("URL");

        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

        try {
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type","application/json; charset=UTF-8");
            conn.setDoOutput(true);
            conn.getResponseCode();
        } catch (ProtocolException e) {
             e.printStackTrace();
        } catch (IOException e) {
            System.out.println(e);
        }
        conn.disconnect();
    }

The exception happens in

  

} catch (IOException e) {

When trying to do

  

conn.getResponseCode ();

    
asked by anonymous 14.09.2015 / 15:31

1 answer

2

You're probably running this code in runtime of Java version 8. SSLv2 is stale for a long time, so it is no longer supported by default in runtime from version 8, not having a socket factory to do so.

You have some options in this scenario:

  • downgrade to a runtime version 7;
  • force the use of SSLv3 by retrieving a TLSv1 context that uses SSLv3 . An example would be this:
final HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

final SSLContext ctx = SSLContext.getInstance("TLSv1");
ctx.init(null, null, null);
conn.setSSLSocketFactory(ctx.getSocketFactory());

You can do globally too, using this:

System.setProperty("https.protocols", "TLSv1");

One note is: here is giving the error No name matching app.xxx.net found , which is when the server's certificate information is different from the host on which it is trying to connect. In your environment you may need to set up a HostnameVerifier . This answers this.

I tested it on another HTTPS server and it's OK, only this one has a problem.

    
14.09.2015 / 16:44