flutter dio自定义http client
final dio = Dio(); Dio getMyDio() { initAdapter(); dio.options.headers = {'apiKey': 'xxxxx'}; dio.options.connectTimeout = const Duration(seconds: 15); dio.options.receiveTimeout = const Duration(seconds: 15); dio.options.validateStatus = (_) => true; return dio; } void initAdapter() { dio.httpClientAdapter = IOHttpClientAdapter( onHttpClientCreate:(client) { final HttpClient client = HttpClient(context: SecurityContext(withTrustedRoots: false)); client.badCertificateCallback = (cert, host, port) => true; return client; }, ); }
用flutter Dio实现适配器属性的自定义
用类似的配置,给dio生成自定义的httpClient,效果类似Android入门教程 | OkHttp + Retrofit 使用 - 掘金 (juejin.cn)中使用retrofit和okhttp设置自己的httpClient并信任所有证书。