flutter 配置charles抓包

一:创建网络请求代理类:

class MyProxyHttpOverride extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    // TODO: implement createHttpClient
    return super.createHttpClient(context)
      ..findProxy = (uri) {
        return "PROXY 10.0.0.228:8888;";
      }
      ..badCertificateCallback = null;
  }
}

二: 使用代理:

Future<void> main() async {
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
  debugPaintSizeEnabled = false;
  /// 使用网络代理
  HttpOverrides.global = MyProxyHttpOverride();
  try {
    WidgetsFlutterBinding.ensureInitialized();
    await DBManager.init();

    cameras = await availableCameras();

    print("cameras has inited");
    // if (Global.localDB == null) {
    //   Global.localDB = await Global.getLocalDataObj();
    //   print("拿到本地的sqlite");
    // }
  } on CameraException {
    print("Error in fetching the cameras");
  } on Exception catch (e) {
    print("main dart e is $e");
  }
  runApp(const MyApp());
}

猜你喜欢

转载自blog.csdn.net/lck8989/article/details/126940378