2

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ru4ert
  • 998
  • 2
  • 14
  • 25

2 Answers2

0

This is a problem in establishing the connection between the device and the mobile. Mainly happened because of used TLS version that the server support and use and the device connected client software isn't supported. For example I faced the same issue with my application I set my Nginx server configuration to use only TLS v1.3 while the okhttpclient on old android OS versions like android 8 in my case 8 supports TLS v1.2 so you have two solutions here:

  1. To change the server TLS version (which I don't recommend for you )
  2. Try to make okhttp work with TLSv1.3 take a look here How to enable TLSv1.3 for OkHttp 3.12.x on Android 8/9?
Mohammad Desouky
  • 734
  • 7
  • 15
-3
[...]
                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

ru4ert
  • 998
  • 2
  • 14
  • 25