gpt4 book ai didi

dart - 如何在执行 POST 请求时解决 Flutter CERTIFICATE_VERIFY_FAILED 错误?

转载 作者:太空宇宙 更新时间:2023-11-03 14:24:17 24 4
gpt4 key购买 nike

我正在用 Dart 发送一个 post 请求。在Postman等API测试工具上测试时,是有响应的。但是当我运行应用程序时。它给了我以下错误:-

E/flutter ( 6264): HandshakeException: Handshake error in client (OS Error: E/flutter ( 6264):  CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:363))

这是我的函数代码-

Future getAccessToken(String url) async {

try {
http.post('url',
body: {
"email": "xyz@xyz.example",
"password": "1234"
}).then((response) {
print("Reponse status : ${response.statusCode}");
print("Response body : ${response.body}");
var myresponse = jsonDecode(response.body);
String token = myresponse["token"];
});
} catch (e) {
print(e.toString());
}

这是完整的错误正文:

E/flutter ( 6264): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception: E/flutter ( 6264): HandshakeException: Handshake error in client (OS Error: E/flutter ( 6264):   CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:363)) E/flutter ( 6264): #0      IOClient.send (package:http/src/io_client.dart:33:23) E/flutter ( 6264): <asynchronous suspension> E/flutter ( 6264): #1      BaseClient._sendUnstreamed (package:http/src/base_client.dart:169:38) E/flutter ( 6264): <asynchronous suspension> E/flutter ( 6264): #2     BaseClient.post (package:http/src/base_client.dart:54:7) E/flutter ( 6264): #3      post.<anonymous closure> (package:http/http.dart:70:16) E/flutter ( 6264): #4      _withClient (package:http/http.dart:166:20) E/flutter ( 6264): <asynchronous suspension> E/flutter ( 6264): #5     post (package:http/http.dart:69:5) E/flutter ( 6264): #6
_MyLoginFormState.getAccessToken (package:chart/main.dart:74:7) E/flutter ( 6264): <asynchronous suspension> E/flutter ( 6264): #7
_MyLoginFormState.build.<anonymous closure> (package:chart/main.dart:64:29)

最佳答案

为了在您的项目中全局启用此选项,您需要执行以下操作:

  1. 在您的 main.dart 文件中,添加或导入以下类:
 import 'dart:io';
 class MyHttpOverrides extends HttpOverrides{
@override
HttpClient createHttpClient(SecurityContext? context){
return super.createHttpClient(context)
..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
}
}
  1. 在您的主函数中,在函数定义之后添加以下行:
 HttpOverrides.global = MyHttpOverrides();

你的 main.dart 应该是这样的

void main() {
// Your code

HttpOverrides.global = MyHttpOverrides();
runApp(const ConsultationApp());
}

This评论对解决这个问题很有帮助,请注意......

This should be used while in development mode, do NOT do this when you want to release to production, the aim of this answer is tomake the development a bit easier for you, for production, you need to fix your certificate issue and use it properly, look at the other answers for this as it might be helpful for your case.

关于dart - 如何在执行 POST 请求时解决 Flutter CERTIFICATE_VERIFY_FAILED 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58281443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com