如何解决此错误 CERTIFICATE_VERIFY_FAILED:无法获取本地颁发者证书(handshake.cc:359))

How to Solve this error CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:359))

我是 flutter 和开发应用程序的新手,但我面临一个关于 asp.net 核心 API 方法的 post 请求的问题。 下面是我用来 post 数据到 asp.net 核心 API 方法的 flutter 代码。

Future registerUser(
    String userFullName, String username, String email, String password) async {
  var body = jsonEncode({
    "UserFullName": userFullName,
    "Username": username,
    "Email": email,
    "Password": password
  });
  final response = await http.post(
      Uri.parse(GetURI.baseURI() + 'Account/Register'),
      body: body,
      headers: {"content-type": "application/json"});
  print("Hello Body! " + response.statusCode.toString());
  print(response.body);
  return null;
}

单击按钮时,我将上述函数调用到 post 数据,但出现错误。单击按钮的代码如下:

onPressed: () async {
                  final validationSuccess = _formKey.currentState!.validate();
                  if (validationSuccess) {
                    _formKey.currentState!.save();
                    final formData = _formKey.currentState!.value;
                    final userData = await registerUser(
                        _formKey.currentState!.fields['username']!.value,
                        _formKey.currentState!.fields['email']!.value,
                        _formKey.currentState!.fields['email']!.value,
                        _formKey.currentState!.fields['password']!.value);
                    print(userData!.userFullName);
                    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                      content: Text('$formData'),
                      duration: Duration(seconds: 5),
                    ));
                  }
                }

单击按钮时出现如下错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error:
CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:359))

这是我返回 URL 的 Class 代码:

class GetURI {
  static String baseURI() {
    return 'https://10.103.78.29:44318/api/';
  }
}

我的main.dart文件

import 'dart:io';

import 'package:bugsmashmobileapp/Screen/LoginScreen/login_body.dart';
import 'package:bugsmashmobileapp/Screen/SignupScreen/signup_body.dart';
import 'package:bugsmashmobileapp/Screen/WelcomeScreen/getting_started_screen.dart';
import 'package:flutter/material.dart';

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

void main() {
  HttpOverrides.global = new MyHttpOverrides();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'BUGSMASH APP',
      theme: ThemeData(
          primarySwatch: Colors.blue, scaffoldBackgroundColor: Colors.white),
      home: GettingStartedScreen(),
      routes: {
        SignupScreen.routeName: (context) => SignupScreen(),
        LoginScreen.routeName: (context) => LoginScreen()
      },
    );
  }
}

我尝试了很多方法并进行了很多搜索,但没有找到解决此错误的好方法。如果有人能提供帮助,我们将不胜感激。

当我尝试 ngrok 时,问题就解决了。 首先,我在 ngrok (Click here to go to website) 上创建了一个帐户,然后您需要安装它的软件以生成一个 URL 并将其转发到您的 API项目。然后你就可以毫无问题地在你的 Flutter 项目中使用 URL。