调用 http post 请求时出错。 ClientException(无法解析 HTTP,67 与 13 不匹配)
Error occuring when calling http post request. ClientException (Failed to Parse HTTP, 67 does not match 13)
我正在尝试执行这个简单的 http 请求,但我总是收到此错误,我也已经尝试过使用 dio 包。
Error.png
其余的api来自客户,但我已经尝试过邮递员的相同请求并且有效。代码:
try {
dynamic response = await http.post(
"https://api/signup",
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode({}),
);
return response;
} catch (e) {
print(e);
}
颤振医生
[√] Flutter (Channel stable, 1.22.4, on Microsoft Windows [versão 10.0.19041.630], locale pt-BR)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[!] Android Studio (version 3.4)
X Dart plugin not installed; this adds Dart specific functionality.
X Unable to determine bundled Java version.
[!] Android Studio (version 4.1.0)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
[√] VS Code, 64-bit edition (version 1.51.1)
[!] Connected device
! No devices available
! Doctor found issues in 3 categories.
我在这里评论我们为这个问题找到的解决方案,以便任何面临类似问题的人都可以解决它。
问题是 dart http 包不完全支持 HTTP 1.1。在 HTTP 1.1 中接收 Transfer-encoding: chunked 是允许的,而在 http 1.0 中则不是这样。那是什么意思?当您发出 http 1.1 请求时,您可能不会立即接收数据,而是分块接收数据。这样 Content-length 将为空,http 客户端应连接所有块并动态计算 content-length。这就是这个包没有做的。它会得到 header Transfer-encoding: chunked 并忽略它,它后面显然会跟着一个 null Content-length header,在 content-length 会引发 ClientException不匹配第一个chucnk 67的值不匹配13。
所以解决方案是: - 添加对 http 1.1 的支持(包括分块响应)到这个包或者破解它并强制这个客户端降级到 http 1.0(没有分块响应)或者......做我们从那以后所做的事情我们可以访问网络服务器并添加“chunked_transfer_encoding off;”在 location ~ .php { nginx 中的块内。如果你不使用 nginx,你的网络服务器可能有类似的选项。
我正在尝试执行这个简单的 http 请求,但我总是收到此错误,我也已经尝试过使用 dio 包。
Error.png
其余的api来自客户,但我已经尝试过邮递员的相同请求并且有效。代码:
try {
dynamic response = await http.post(
"https://api/signup",
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode({}),
);
return response;
} catch (e) {
print(e);
}
颤振医生
[√] Flutter (Channel stable, 1.22.4, on Microsoft Windows [versão 10.0.19041.630], locale pt-BR)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[!] Android Studio (version 3.4)
X Dart plugin not installed; this adds Dart specific functionality.
X Unable to determine bundled Java version.
[!] Android Studio (version 4.1.0)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
[√] VS Code, 64-bit edition (version 1.51.1)
[!] Connected device
! No devices available
! Doctor found issues in 3 categories.
我在这里评论我们为这个问题找到的解决方案,以便任何面临类似问题的人都可以解决它。 问题是 dart http 包不完全支持 HTTP 1.1。在 HTTP 1.1 中接收 Transfer-encoding: chunked 是允许的,而在 http 1.0 中则不是这样。那是什么意思?当您发出 http 1.1 请求时,您可能不会立即接收数据,而是分块接收数据。这样 Content-length 将为空,http 客户端应连接所有块并动态计算 content-length。这就是这个包没有做的。它会得到 header Transfer-encoding: chunked 并忽略它,它后面显然会跟着一个 null Content-length header,在 content-length 会引发 ClientException不匹配第一个chucnk 67的值不匹配13。 所以解决方案是: - 添加对 http 1.1 的支持(包括分块响应)到这个包或者破解它并强制这个客户端降级到 http 1.0(没有分块响应)或者......做我们从那以后所做的事情我们可以访问网络服务器并添加“chunked_transfer_encoding off;”在 location ~ .php { nginx 中的块内。如果你不使用 nginx,你的网络服务器可能有类似的选项。