如何在执行 POST 请求时解决颤振 CERTIFICATE_VERIFY_FAILED 错误? [英] how to solve flutter CERTIFICATE_VERIFY_FAILED error while performing a POST request?

查看:30
本文介绍了如何在执行 POST 请求时解决颤振 CERTIFICATE_VERIFY_FAILED 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Dart 中发送一个发布请求.当我在 API 测试工具(如 Postman)上对其进行测试时,它会给出响应.但是当我运行应用程序时.它给了我以下错误:-

E/flutter (6264): HandshakeException: Handshake error in client (OS Error: E/flutter (6264): CERTIFICATE_VERIFY_FAILED: 无法获得本地颁发者证书(handshake.cc:363))

这是我的函数代码 -

未来 getAccessToken(String url) async {尝试 {http.post('url',身体: {"email": "xyz@xyz.com",密码":1234"}).then((响应){print("回复状态:${response.statusCode}");print("响应正文:${response.body}");var myresponse = jsonDecode(response.body);String token = myresponse["token"];});}赶上(e){打印(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)错误:E/flutter (6264): CERTIFICATE_VERIFY_FAILED: 无法获得本地颁发者证书(handshake.cc:363)) E/flutter (6264): #0 IOClient.send (package:http/src/io_client.dart:33:23)E/flutter(6264):<异步悬挂>E/flutter(6264):#1 BaseClient._sendUnstreamed(包:http/src/base_client.dart:169:38)E/flutter(6264):<异步暂停>E/flutter (6264): #2 BaseClient.post (package:http/src/base_client.dart:54:7) E/flutter (6264): #3 post.<匿名闭包>(package:http/http.dart:70:16) E/flutter (6264): #4 _withClient (package:http/http.dart:166:20) E/flutter (6264): <异步暂停>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):<异步暂停>E/颤振(6264):#7_MyLoginFormState.build.<匿名闭包>(包:图表/main.dart:64:29)

解决方案

为了让 Flutter/Dart 的新手特别清楚,为了在项目中全局启用此选项,您需要执行以下操作:

  1. 在您的 main.dart 文件中,添加或导入以下类:

<块引用>

 class MyHttpOverrides 扩展 HttpOverrides{@覆盖HttpClient createHttpClient(SecurityContext?context){返回 super.createHttpClient(context)..badCertificateCallback = (X509Certificate cert, String host, int port)=>真的;}}

  1. 在您的主函数中,在函数定义之后添加以下行:

<块引用>

HttpOverrides.global = MyHttpOverrides();

这个评论对通过这件事很有帮助,请注意...

<块引用>

这应该在开发模式下使用,不要在你想要发布到生产环境时这样做,这个答案的目的是强>让您的开发更容易一些,对于生产,您需要修复证书问题并正确使用它,请查看其他答案,因为它可能对您的情况有所帮助.

I am sending a post request in Dart. It is giving a response when I test it on API testing tools such as Postman. But when I run the app. It gives me the following error:-

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))

Here is my code of the function -

Future getAccessToken(String url) async {

    try {
      http.post('url',
          body: {
            "email": "xyz@xyz.com",
            "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());
    }

Here's the full error body:

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)

解决方案

Just for the sake of clarity specially for the newcomers to Flutter/Dart, here is what you need to do in order to enable this option globally in your project:

  1. In your main.dart file, add or import the following class:

 class MyHttpOverrides extends HttpOverrides{
  @override
  HttpClient createHttpClient(SecurityContext? context){
    return super.createHttpClient(context)
      ..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
  }
}

  1. In your main function, add the following line after function definition:

HttpOverrides.global = MyHttpOverrides();

This comment was very helpful to pass through this matter, and please note that...

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 to make 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.

这篇关于如何在执行 POST 请求时解决颤振 CERTIFICATE_VERIFY_FAILED 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆