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并信任所有证书。

posted @ 2023-05-22 09:00  姜劭  阅读(334)  评论(0编辑  收藏  举报