【问题标题】:How to Solve this error CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:359))如何解决此错误 CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:359))
【发布时间】:2021-09-18 21:35:26
【问题描述】:

我是 Flutter 和开发应用程序的新手,但我遇到了关于对 asp.net 核心 API 方法的发布请求的问题。 下面是我用来将数据发布到 asp.net 核心 API 方法的颤振代码。

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;
}

单击按钮时,我正在调用上述函数来发布数据,但出现错误。按钮点击代码如下:

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 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()
      },
    );
  }
}

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

【问题讨论】:

  • 您正在尝试调用一个不安全的 URL,因此您需要为 badCertificateCallBack stackoverflow.com/questions/54285172/… 覆盖颤振安全性
  • 是的,我已经完成了这项工作,但它对我不起作用。我已经编辑了我的问题@Einzeln,你现在可以看到它。

标签: android flutter ssl-certificate http-post sslhandshakeexception


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-09-07
    • 2016-06-28
    • 2022-06-16
    • 1970-01-01
    • 2011-05-22
    • 2017-07-10
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    相关资源
    最近更新 更多