本文介绍了Flutter中自签名证书的SSL握手错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用自签名证书连接服务器,但出现错误:
E/flutter(3781):HandshakeException:客户端中的握手错误(操作系统错误:
E/flutter(3781):CERTIFICATE_VERIFY_FAILED:主机名不匹配(ssl_cert.c:345))
代码,我在其中设置证书:
I'm trying to connect server with self-signed cert, but I take error:
E/flutter ( 3781): HandshakeException: Handshake error in client (OS Error:
E/flutter ( 3781): CERTIFICATE_VERIFY_FAILED: Hostname mismatch(ssl_cert.c:345))
Code, where I set cert:
String path = '/storage/sdcard0/server.crt';
SecurityContext context = new SecurityContext();
context.setTrustedCertificates(path, password: 'hello');
_client = new HttpClient(context: context);
我做错了什么?
如果未设置SecurityContext,则会收到SSL握手错误.
If I don't set SecurityContext, I get SSL handshake error.
推荐答案
我使用了 HttpClient.badCertificateCallback
这是接受任何证书的代码:
I used HttpClient.badCertificateCallback
Here is a code to accept any cert:
_client = new HttpClient();
_client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;
这篇关于Flutter中自签名证书的SSL握手错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!