Android 颤振中自签名证书上的SSL握手错误

Android 颤振中自签名证书上的SSL握手错误,android,ssl,flutter,Android,Ssl,Flutter,我正在尝试使用自签名证书连接服务器,但出现错误: E/颤振(3781):握手异常:客户端握手错误(操作系统错误: E/flatter(3781):证书验证失败:主机名不匹配(ssl证书c:345)) 代码,我在其中设置证书: String path = '/storage/sdcard0/server.crt'; SecurityContext context = new SecurityContext(); context.setTrustedCertificates(path, passwo

我正在尝试使用自签名证书连接服务器,但出现错误:
E/颤振(3781):握手异常:客户端握手错误(操作系统错误:
E/flatter(3781):证书验证失败:主机名不匹配(ssl证书c:345))
代码,我在其中设置证书:

String path = '/storage/sdcard0/server.crt';
SecurityContext context = new SecurityContext();
context.setTrustedCertificates(path, password: 'hello');
_client = new HttpClient(context: context);
我做错了什么


如果我没有设置SecurityContext,就会出现SSL握手错误。

您可以从我使用的免费获得有效的SSL证书
以下是接受任何证书的代码:

_client = new HttpClient();
_client.badCertificateCallback = (X509Certificate cert, String host, int port) => true;

在我的例子中,我收到了这个错误消息,因为在创建自签名证书时,当要求公共名称时,我没有指定主机名(对于简单的测试,localhost是可以的):


谢谢是否有任何变量可以像java中那样设置“对所有证书的信任”之类的内容?请参见另一个答案。我确实想知道,如果你不想安全,为什么要加密呢?我能想到一些原因,但从你的问题看不出来。我正在使用自签名证书进行开发,现在不需要使用真正的证书。HttpClient使用具有有效证书的站点,我对它们没有问题。@ArmenKH。您使用的是哪台web服务器?certbot是一个被低估的工具。您可以发布更多的代码吗?它可以工作,但不能回答最初的问题。接受所有证书是一个错误的选择。我遇到了同样的问题,不明白为什么设置“setTrustedCertificates”不起作用。
$ openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.pem

Country Name (2 letter code) [AU]:SI
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []: