Help Need: Handshake error in client

Hi,

I am trying to connect to a signalR service and encountered with an error as below.

HandshakeException: Handshake error in client (OS Error:
I/flutter ( 6985): │ :no_entry: CERTIFICATE_VERIFY_FAILED: Hostname mismatch(handshake.cc:393))

Any help on this?

Hi,

We’re also seeing this error, if a user is running a VPN disabling it can sometimes help.

This issue on GitHub may be related:

SSL certificates are explicitly tied to hostnames. If you’re connecting to serverA.com and you get a certificate that says serverB.com, you’ll see this error.

The answer to why you’re getting a mismatched hostname in the certificate could be a million different reasons. I know that, for myself, I use a secure DNS server with a blocklist. If I try to connect to a site on the blocklist, my DNS server will return a block page with an SSL certificate (that doesn’t match the original hostname). This is just one example of what could be happening.

1 Like

To solve this issue use badCertificateCallback To temporarily bypass SSL validation during development, you can override the default HTTP behavior :

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

in main.dart and use it as

void main(){
WidgetsFlutterBinding.ensureInitialized(); // use it here
  runApp(GetMaterialApp(
      title: '',
      theme: ThemeData(
        useMaterial3: true,
        primarySwatch: Colors.blue,
      ),
      debugShowCheckedModeBanner: false,

      home: Screen() ,
    ));
}

Note : This approach should only be used for testing or development environments.
It disables SSL certificate validation, which makes the app vulnerable to security threats such as man-in-the-middle (MITM) attacks. Never use this method in production builds

Also I think the override doesn’t work if you use the new native http_clients

You’re absolutely right — the standard override approach work if you’re using the new native HTTP client introduced via :

import ‘dart:io’;
final client = HttpClient(); // or native-style HTTP under the hood