Use this code.
import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter/services.dart';
var sccontext = SecurityContext.defaultContext;
String data = await rootBundle.loadString("assets/cert.pem");
//it can be "cert.crt" as well.
List bytes = utf8.encode(data);
sccontext.setTrustedCertificatesBytes(bytes);
HttpClient client = new HttpClient(context: sccontext);
Remember that set the security context just once.Do not set the security context in each and every call of http request.
Hope it helps
这段代码里的"assets/cert.pem"是资源文件,意思是打包到app里的,和你说的建立https连接之后,将证书保存下来,好像不一样。
如果证书提前创建好就可以,这个cert.pem怎么生成呢,
抱歉,这块不太懂哈哈
【 在 keygen 的大作中提到: 】
: 或者成功建立https连接之后,将证书保存下来,下次建立连接之前用 SecurityContext 直接信任这个证书。
: 参考这个:
https://github.com/flutterchina/dio/issues/612#issuecomment-569216226: let's encrypt 的证书三个月到期,一般两个月左右更新一次,一两个月卡顿一下我觉得没什么问题。
: ...................
--
FROM 180.174.54.*