我的服务器正在运行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)
...
答案 0 :(得分:5)
正如@Patrick在评论中提到的,这是TLS不匹配的结果。
一般经验法则:如果服务器(API)基于https
(TLS),则客户端应使用https
进行连接。如果服务器使用http
(非TLS),则客户端应使用http
连接到服务器。
在您的情况下,您似乎要尝试使用的API是http
类型,因此在您的Flutter应用中,您应该使用:
Uri.http(baseUrl, endPointUrl)
答案 1 :(得分:0)
将此添加到您的 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"
答案 2 :(得分:-2)
我认为您使用的是https://,但尚未安装SSL证书。因此,请尝试使用http://而不是https://。 以我为例。 希望也能在你这边工作。