我收到了"'CERTIFICATE_VERIFY_FAILED:Flutter中的主机名不匹配“”错误

gab6jxml  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(283)

我试图将MQTT结构集成到Flutter应用程序中,结果出现错误'CERTIFICATE_VERIFY_FAILED:主机名不匹配(handshake.cc:393)。'我提供了一个来自MQTT本身的证书,但无论我怎么尝试,我都无法让它被接受。
这是我的密码

Future<void> initializeMQTTClient() async {
    _client = MqttServerClient(_host, '');
    final ByteData caByteData = await rootBundle.load('assets/ca.crt');
    final caCrt = caByteData.buffer.asUint8List();
    final context = SecurityContext.defaultContext;

    _client!.securityContext.setTrustedCertificatesBytes(caCrt);
   // _client!.securityContext.setTrustedCertificates('assets/ca.crt');
    _client!.port = 51883;
    _client!.keepAlivePeriod = 20;
    _client!.onDisconnected = onDisconnected;
    _client!.secure = true;
    _client!.logging(on: true);
    _client!.connect('emirhan', 'deneme');
    _client!.onConnected = onConnected;
    _client!.onSubscribed = onSubscribed;

    final MqttConnectMessage connMess = MqttConnectMessage()
        .withClientIdentifier('test')
        .withWillTopic(
            'willtopic') // If you set this you must set a will message
        .withWillMessage('My Will message')
        .authenticateAs('emirhan', 'deneme')
        .startClean() // Non persistent session for testing
        .withWillQos(MqttQos.atLeastOnce);
    print('EXAMPLE::Mosquitto2 client connecting....');
    _client!.connectionMessage = connMess;
  }

字符串
我在不同的项目中使用了不同的代码库,但我在MQTT的Flutter中很难取得进展,因为没有太多可用的资源。

ev7lccsx

ev7lccsx1#

通常这意味着证书中的主机名实际上与您试图连接的主机名不同。例如,证书用于本地主机,但主机为randomname.com

相关问题