简体   繁体   English

当我从Android向Flask Web服务发送参数时,如何解决“ SSL库故障”?

[英]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. 我确实尝试将一些值作为“ application / json ”从Android应用程序发送到Flask Web服务。 this is my Java code : 这是我的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 code : 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)

but it shows this error (flask): 但它显示此错误(烧瓶): 在此处输入图片说明

so I did track the error on android and I found this error: 所以我确实在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$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()); 问题是android不再支持SSLv3,因此我确实尝试使用ProviderInstaller.installIfNeeded(getApplicationContext())删除它 at the beginning of the activity but no use. 在活动开始时没有用。 so help and thanks 所以帮助和感谢

You are sending the request as "POST" from your Android application but on Python side, the route you created will accept "GET". 您正在从Android应用程序以“ POST”形式发送请求,但在Python方面,您创建的路由将接受“ GET”。

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

conn.setRequestMethod("POST");

Try changing @app.route('/a/', methods=['GET']) with @app.route('/a/', methods = ['POST']) 尝试使用@app.route('/a/', methods=['GET']) @app.route('/a/', methods = ['POST'])更改@app.route('/a/', methods=['GET']) @app.route('/a/', methods = ['POST'])

Also seems like you are trying to connect to HTTPS: 似乎您正在尝试连接到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') 尝试如下创建应用程序实例: if __name__ == '__main__': app.run(port=5000, host='0.0.0.0', debug=True, ssl_context='adhoc')

pip install pyopenssl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM