Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handshake error in client : CERTIFICATE_VERIFY_FAILED Self signed certificate in flutter

I am making an https post Request from my flutter app. as there I am using a self signed SSL certificate in server so when I hit the API I am receiving status code as 405, that I am not able to connect,

If I used HTTP package, I am getting bellow exception,

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

when I tried with deo package I am getting 405 status code, below is the code for that,

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

I tried to avoid the SSL handshake by making

 client.badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;

Still its not working any other solution?

like image 635
mark Avatar asked Apr 29 '20 15:04

mark


1 Answers

This works for me

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 {
.....
like image 142
Laurent Avatar answered Sep 23 '22 08:09

Laurent