Search code examples
javaandroidssltls1.2sslv3

How can solve "Failure in SSL library" when I send params to a Flask web service from Android?


I did try to send some values as "application/json" from Android application to Flask web-service. this is my Java code:

  Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL("https://192.168.43.31:5000/a/");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                conn.setRequestProperty("Accept","application/json");
                conn.setDoOutput(true);
                conn.setDoInput(true);
                JSONObject jsonParam = new JSONObject();
                jsonParam.put("name", name);
                Log.i("JSON", jsonParam.toString());
                DataOutputStream os = new DataOutputStream(conn.getOutputStream());
                //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
                os.writeBytes(jsonParam.toString());
                os.flush();
                os.close();
                Log.i("STATUS", String.valueOf(conn.getResponseCode()));
                Log.i("MSG" , conn.getResponseMessage());
                conn.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();

python code:

    #!flask/bin/python
    from flask import Flask , request


       app = Flask(__name__)
       @app.route('/a/', methods=['GET', 'POST'])
       def get_request1():

          data = request.data


       if __name__ == '__main__':
           app.run(host="0.0.0.0",debug=True, port=5000)

but it shows this error (flask): enter image description here

so I did track the error on android and I found this error:

I/System.out: open:https://192.168.43.31:5000/a/
I/JSON: {"name":"gshzj"}
D/libc-netbsd: [getaddrinfo]: hostname=192.168.43.31; servname=(null); cache_mode=(null), netid=0; mark=0
           [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
I/System.out: [CDS][DNS] getAllByNameImpl netId = 0
D/libc-netbsd: [getaddrinfo]: hostname=192.168.43.31; servname=(null); cache_mode=(null), netid=0; mark=0
           [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
I/System: core_booster, getBoosterConfig = false
I/System.out: [CDS]rx timeout:0
I/System.out: [socket][0] connection /192.168.43.31:5000;LocalPort=34295(0)
          [CDS]connect[/192.168.43.31:5000] tm:90
D/Posix: [Posix_connect Debug]Process com.sourcey.materialloginexample :5000 
I/System.out: [socket][/192.168.43.1:34295] connected
I/System: core_booster, getBoosterConfig = false
D/libc-netbsd: [getaddrinfo]: hostname=192.168.43.31; servname=(null); cache_mode=(null), netid=0; mark=0
           [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
D/Surface: Surface::setBuffersDimensions(this=0xb45af700,w=720,h=1184)
V/NativeCrypto: SSL handshake aborted: ssl=0xaf8326c8: Failure in SSL library, usually a protocol error
            error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER (third_party/openssl/boringssl/src/ssl/tls_record.cc:242 0x9b4bb9fe:0x00000000)
D/Surface: Surface::setBuffersDimensions(this=0xb45af700,w=720,h=1184)
I/System.out: [CDS]close[34295]
          Close in OkHttp
I/System: core_booster, getBoosterConfig = false
I/System.out: [CDS]rx timeout:0
          [socket][1] connection /192.168.43.31:5000;LocalPort=36619(0)
          [CDS]connect[/192.168.43.31:5000] tm:90
D/Posix: [Posix_connect Debug]Process com.sourcey.materialloginexample :5000 
I/System.out: [socket][/192.168.43.1:36619] connected
I/System: core_booster, getBoosterConfig = false
I/System.out: [CDS]close[36619]
W/System.err: javax.net.ssl.SSLHandshakeException: No enabled protocols; SSLv3 is no longer supported and was filtered from the list
W/System.err:     at com.google.android.gms.org.conscrypt.NativeSsl.initialize(:com.google.android.gms@[email protected] (020306-194189626):7)
              at com.google.android.gms.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(:com.google.android.gms@[email protected] (020306-194189626):6)
              at com.android.okhttp.Connection.upgradeToTls(Connection.java:201)
              at com.android.okhttp.Connection.connect(Connection.java:155)
              at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:282)
              at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:216)
              at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:392)
              at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:107)
              at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:218)
              at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
              at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
              at com.sourcey.materiallogindemo.SignupActivity$4.run(SignupActivity.java:218)
              at java.lang.Thread.run(Thread.java:831)

the problem is that SSLv3 is not supported anymore by android, so I did try to remove it using ProviderInstaller.installIfNeeded(getApplicationContext()); at the beginning of the activity but no use. so help and thanks


Solution

  • You are sending the request as "POST" from your Android application but on Python side, the route you created will accept "GET".

    @app.route('/a/', methods=['GET'])

    conn.setRequestMethod("POST");

    Try changing @app.route('/a/', methods=['GET']) with @app.route('/a/', methods = ['POST'])

    Also seems like you are trying to connect to HTTPS:

    URL url = new URL("https://192.168.43.31:5000/a/");

    Try creating the app instance like below: if __name__ == '__main__': app.run(port=5000, host='0.0.0.0', debug=True, ssl_context='adhoc')

    pip install pyopenssl