代码之家  ›  专栏  ›  技术社区  ›  ahmedhelali100

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

  •  0
  • ahmedhelali100  · 技术社区  · 6 年前

    我确实尝试发送一些值作为“ 应用程序/json “从 安卓 应用于 瓶子 web服务。 这是我的 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)
    

    但它显示了此错误(烧瓶): enter image description here

    所以我在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)
    

    问题是,android不再支持SSLv3,所以我尝试使用 ProviderInstaller。installIfNeeded(getApplicationContext()); 在活动开始时,但没有使用。 所以请帮忙 谢谢你

    1 回复  |  直到 6 年前
        1
  •  1
  •   Tejashwi Kalp Taru    6 年前

    您正在从Android应用程序以“POST”的形式发送请求,但在Python方面,您创建的路由将接受“GET”。

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

    conn.setRequestMethod("POST");

    试着改变 @应用程序。路由(“/a/”,方法=[“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