flutter - 客户端握手错误 : CERTIFICATE_VERIFY_FAILED Self signed certificate in flutter

标签 flutter ssl dart dio

我正在从我的 flutter 应用程序发出 https 帖子请求。因为我在服务器中使用自签名 SSL 证书,所以当我点击 API 时,我收到状态码为 405,我无法连接,

如果我使用 HTTP 包,我会收到以下异常,

HandshakeException: Handshake error in client (OS Error: I/flutter ( 7107): CERTIFICATE_VERIFY_FAILED: self signed certificate(handshake.cc:352))

当我尝试使用 deo 包时,我得到 405 状态码,下面是它的代码,
Response response;
    final Dio dio = Dio();
    (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
    (HttpClient client) {
  client.badCertificateCallback =
      (X509Certificate cert, String host, int port) => true;
  return client;
};
  response = await dio.post(loginURL, data: {"username": username, "password": password});
  print(response.data.toString());
  print(response.statusCode);

我试图通过制作来避免 SSL 握手
 client.badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;

仍然无法使用任何其他解决方案?

最佳答案

这对我有用

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

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

class MyApp extends StatelessWidget {
.....

关于flutter - 客户端握手错误 : CERTIFICATE_VERIFY_FAILED Self signed certificate in flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61505097/

相关文章:

flutter - 如何使用 StreamBuilder 更新 TextField 的值?

flutter - flutter 显示数据库中的多行字符串被切断

ssl - 从 outlook imap 服务器获取 offlineimap 的正确证书信息

flutter - 如何使用 flutter bloc 通过使用 firestore 的依赖注入(inject)来处理错误 `The getter was called on null`

android - 如果我的函数调用异步函数,是否会迫使我让我的函数返回 Future?

flutter - 此 ValueListenableBuilder 小部件无法标记为需要构建

iOS 使用自签名 TLS/SSL 证书连接不起作用

ssl - handshake_failure 试图访问同一服务器上的文件

dart - DropdownButton不会更改onChanged上的值

android - flutter : RenderBox was not laid out: RenderRepaintBoundary#58c65 relayoutBoundary=up1 NEEDS-PAINT