继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Flutter dio如何设置网络代理

唯你Only
关注TA
已关注
手记 32
粉丝 8
获赞 22

dio版本是4.0.6设置办法

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;
  };
}

如果觉得文章对你有帮助,点赞、收藏、关注、评论,一键四连支持,你的支持就是我创作最大的动力。

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP