Java Android Developing "Failure in SSL library, usually a protocol"

1

I'm programming an Application just for testing with Android Studio. I want to Request a Server with POST params which are in the string: (strings[0]).

But I always get this Exception:

javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x6f427ff108: Failure in SSL library, usually a protocol error error:1000042e:SSL routines:OPENSSL_internal:TLSV1_ALERT_PROTOCOL_VERSION (external/boringssl/src/ssl/tls_record.cc:587 0x6fa8195d88:0x00000001)

I tried also with httpok.

https://square.github.io

The Server works with TLSV1.2

[...]
                byte[] postData = strings[0].getBytes(StandardCharsets.UTF_8);
                int postDataLength = postData.length;

                URL url = new URL("https://example.com");

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

                //
                conn.setDoOutput(true);
                conn.setInstanceFollowRedirects(false);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                conn.setRequestProperty("charset", "utf-8");
                conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
                conn.setUseCaches(false);

                //
                // TLSv1 | TLSv1.1 | TLSv1.2
                SSLContext sc = SSLContext.getInstance("TLSv1.2");
                sc.init(null, null, new java.security.SecureRandom());
                sc.getProtocol();
                sc.getSupportedSSLParameters();
                sc.getDefaultSSLParameters();
                SSLEngine engine = sc.createSSLEngine();


                conn.setSSLSocketFactory(sc.getSocketFactory());

                conn.connect();
                try (final InputStream in = conn.getInputStream()) {
                    //…
                }
[...]

I don't get any further.

java
android
gradle
asked on Stack Overflow Dec 13, 2019 by ru4ert • edited Dec 13, 2019 by Vadim Kotov

1 Answer

0
[...]
                byte[] postData = strings[0].getBytes(StandardCharsets.UTF_8);
                int postDataLength = postData.length;

                URL url = new URL("https://example.com");
  // The url has to be set to an international variable

                new Thread(new Runnable() {
                     public void run() {

                        // The url has to be fetched from an international variable
                        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

                //
                        conn.setDoOutput(true);
                        conn.setInstanceFollowRedirects(false);
                        conn.setRequestMethod("POST");
                        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                        conn.setRequestProperty("charset", "utf-8");
                        conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
                        conn.setUseCaches(false);

                        //
                        // TLSv1 | TLSv1.1 | TLSv1.2
                        SSLContext sc = SSLContext.getInstance("TLSv1.2");
                        sc.init(null, null, new java.security.SecureRandom());
                        sc.getProtocol();
                        sc.getSupportedSSLParameters();
                        sc.getDefaultSSLParameters();
                        SSLEngine engine = sc.createSSLEngine();


                        conn.setSSLSocketFactory(sc.getSocketFactory());

                        conn.connect();
                      });
                    }
                }).start();

                try (final InputStream in = conn.getInputStream()) {
                    //…
                }
[...]

https://developer.android.com/guide/components/processes-and-threads

answered on Stack Overflow May 2, 2020 by ru4ert • edited Jun 6, 2020 by ru4ert

User contributions licensed under CC BY-SA 3.0