客户端中的握手错误:颤振中的 CERTIFICATE_VERIFY_FAILED 自签名证书

mar*_*ark 9 ssl dart flutter dio

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

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

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

当我尝试使用 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);
Run Code Online (Sandbox Code Playgroud)

我试图通过以下方式避免 SSL 握手

 client.badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
Run Code Online (Sandbox Code Playgroud)

仍然没有其他解决方案吗?

Lau*_*ent 9

这对我有用

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 {
.....
Run Code Online (Sandbox Code Playgroud)


Oma*_*att 0

正如前面的评论中提到的,HTTP 请求通过,因为它返回了405 error. HTTP错误405通常被定义为“方法不允许”,通常是由不正确的请求方法引起的。您可能需要检查服务器是否可以接收您发送的请求。