Java Https - Httpsurlconnection - Unsupported Sslv2hello

Asked

Viewed 121 times

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 and) {

while trying to make

Conn.getResponseCode();

  • The problem is the certificate, the code is fine as said. Thanks for the test. I tested again with a different https and it worked. Thanks

1 answer

2

You’re probably running this code on Runtime version 8 of Java. SSLv2 is obsolete it’s been a long time, so it’s no longer supported by default on Runtime version 8, not having a socket Factory for such.

You have some options in this scenario:

  • do downgrade for a Runtime version 7;
  • force the use of Sslv3 by retrieving a context TLSv1 who 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 it globally too, using this:

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

One observation is: here is giving the error No name matching app.xxx.net found, which is when the server certificate information is different from the host which you are trying to connect to. In your environment you may need to set up a HostnameVerifier. This answer deals with this.

I tested on another HTTPS server and is OK, the only one presented problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.