Flutter dio如何设置网络代理

日期: 栏目:文章分享 浏览:689 评论:0

dio版本是4.0.6设置办法

Flutter dio如何设置网络代理-第1张图片-Ceacer网络

import 'package:dio/adapter.dart';import 'package:dio/dio.dart';

Dio dio = Dio();//网络代理设置办法(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
        (HttpClient client) {
      client.badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
      client.findProxy = (uri) {        // 代理,这里localhost:888需要根据实际情况设置相应代理地址
        String proxy = 'PROXY localhost:8888';
        debugPrint('flutter_proxy $proxy');        return proxy;
      };
    };

dio版本5.0.1设置办法

import 'package:dio/io.dart';void initAdapter() {
  dio.httpClientAdapter = IOHttpClientAdapter()..onHttpClientCreate = (client) {    // Config the client.
    client.findProxy = (uri) {      // Forward all request to proxy "localhost:8888".
      return 'PROXY localhost:8888';
    };    // You can also create a new HttpClient for Dio instead of returning,
    // but a client must being returned here.
    return client;
  };
}


标签:

评论留言

我要留言

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。发布前请先查看评论规则:点我查看