当我从 Android 向 Flask Web 服务发送参数时,如何解决 "Failure in SSL library"?

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

我确实尝试将一些值作为“application/json”从 Android 应用程序发送到 Flask 网络服务。 这是我的 Java 代码:

  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代码:

    #!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)

但它显示此错误(烧瓶):

所以我确实跟踪了 android 上的错误,我发现了这个错误:

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@12673008@12.6.73 (020306-194189626):7)
              at com.google.android.gms.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(:com.google.android.gms@12673008@12.6.73 (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.run(SignupActivity.java:218)
              at java.lang.Thread.run(Thread.java:831)

问题是 android 不再支持 SSLv3,所以我确实尝试使用 ProviderInstaller.installIfNeeded(getApplicationContext()); 将其删除开头的activity可是没用。 所以帮助 并感谢

您正在从您的 Android 应用程序以 "POST" 的形式发送请求,但在 Python 端,您创建的路由将接受 "GET".

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

conn.setRequestMethod("POST");

尝试将 @app.route('/a/', methods=['GET']) 更改为 @app.route('/a/', methods = ['POST'])

您似乎也在尝试连接到 HTTPS:

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

尝试像下面这样创建应用程序实例: if __name__ == '__main__': app.run(port=5000, host='0.0.0.0', debug=True, ssl_context='adhoc')

pip install pyopenssl