【问题标题】:Why i'm getting WRONG_VERSION_NUMBER when making a POST request to a HTTPS Server?为什么我在向 HTTPS 服务器发出 POST 请求时收到 WRONG_VERSION_NUMBER?
【发布时间】:2021-05-20 03:56:27
【问题描述】:

我的应用程序向服务器发出大量 HTTP 请求。在其上安装 SSL 证书后,应用程序崩溃了。

我的服务器运行 Ubuntu,Nginx 托管 PHP 代码(使用证书和工作)并“代理”用 NodeJS 编写的应用服务器代码。在我更改为 HTTPS 之前它一直有效。

我的 POST 请求典型用法:

var jsonRequest = //Some JSON.
Map<String, String> headers = {'Content-type': 'application/json'};
var response = await http.post(urls['auth'], body: jsonRequest,headers: headers);

我得到的错误:

E/flutter (25875): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error: 
E/flutter (25875):      WRONG_VERSION_NUMBER(tls_record.cc:242))

E/flutter (25875): #0      IOClient.send (package:http/src/io_client.dart:33:23)
E/flutter (25875): <asynchronous suspension>
E/flutter (25875): #1      BaseClient._sendUnstreamed (package:http/src/base_client.dart:169:38)
E/flutter (25875): <asynchronous suspension>
E/flutter (25875): #2      BaseClient.post (package:http/src/base_client.dart:54:7)
E/flutter (25875): #3      post.<anonymous closure> (package:http/http.dart:70:16)

...

【问题讨论】:

  • 通常情况下,当 HTTPS 客户端到达 HTTP 端点时会发生这种情况,因此根本无法获得正确的 TLS。确保在您使用的 URL 中有正确的 TLS (HTTPS) 服务器。易于使用 curl、wget、openssl s_client 或任何在线 TLS 网络服务器检查器进行测试。
  • 谢谢。我尝试使用 curl 并得到了同样的错误。看起来我直接连接到节点(HTTP)服务器而不是 NGINX,更改了端口并且它消失了。
  • 在颤振 2.0 中通过将 Url(...) 而不是 Url.parse(...) 传递给 http.post 方法修复了相同的问题。

标签: ssl flutter


【解决方案1】:

正如@Patrick 在 cmets 中提到的,这是 TLS 不匹配的结果。

一般经验法则:如果服务器 (API) 基于 https (TLS),则客户端应使用 https 进行连接。如果服务器使用http(非TLS),那么客户端应该使用http来连接它。

在您的情况下,您尝试访问的 API 似乎是 http 类型,因此您应该使用 Flutter 应用程序:

Uri.http(baseUrl, endPointUrl)

【讨论】:

    【解决方案2】:

    我认为您正在使用 https:// 但尚未安装 SSL 证书。所以尝试使用 http:// 而不是 https://。 在我的情况下这项工作。 希望也能为您效劳。

    【讨论】:

      【解决方案3】:

      将此添加到您的 android/app/src/main/AndroidManifest.xml

      android:usesCleartextTraffic="true"
      

      结果是这样的:

      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.contact_wsp">
         <application
              android:label="contact_wsp"
              android:usesCleartextTraffic="true"
              android:icon="@mipmap/ic_launcher">
              <activity
                  android:name=".MainActivity"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-02
        • 1970-01-01
        • 1970-01-01
        • 2021-08-23
        相关资源
        最近更新 更多