flutter dio 想charles抓包的话 必须代码设置下电脑ip才行
写死代码里的话 不方便测试抓包,我写了一个页面
启动app显示出来,输入一次ip后就可以抓包了, ip会保存本地
不删除app就不用每次都输入ip
app启动时候可以先显示这个页面 设置好抓包后再进入
class DevProxyPage extends StatefulWidget {
const DevProxyPage({
super.key,
});
@override
State<DevProxyPage> createState() => _DevProxyPageState();
}
class _DevProxyPageState extends JDState<DevProxyPage> {
String kDevDailiKey = 'kDevDailiKey';
String get hasProxy {
return JDCache.getString(kDevDailiKey) ?? 'false';
}
TextEditingController controller = TextEditingController();
@override
void initState() {
if (hasProxy.isNotEmpty) {
controller.text = hasProxy;
}
super.initState();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('设置Charles网络代理'),
actions: [
TextButton(
onPressed: () {
// TDChangeEnvironmentDialog().showEnvironmentDialog(context);
},
child: const Text('环境设置'))
],
),
body: JDScrollView(
needDismissKeyBord: true,
children: [
50.vGap,
Text(
hasProxy.isNotEmpty ? ' 当前设置--有--代理$hasProxy' : ' 没有设置网络代理',
style: TextStyle(
color: hasProxy.isNotEmpty ? Colors.red : Colors.black),
),
Padding(
padding: const EdgeInsets.all(30),
child: JDTextField(
'输入Charles代理ip',
textEditController: controller,
editingCompleteCallBack: (text) {},
),
),
20.vGap,
JDButton(
text: '代理抓包模式进入首页',
backgroundColor: Colors.red,
onPressed: () {
if (controller.text.isNotEmpty) {
JDCache.setString(kDevDailiKey, controller.text);
}
SetProxyRequest().setProxy(hasProxy, "8888");
Future.delayed(const Duration(milliseconds: 300), () {
jump();
});
},
),
30.vGap,
JDButton(
text: '非代理模式 进入首页',
onPressed: () {
jump();
},
),
],
),
);
}
jump() {
RouterUtil.popUntil(context);
RouterContractPage.instance.splashPage
.push(context, hideAnimation: true, replacement: true);
}
}
// dio网络代理抓包设置
class SetProxyRequest {
void setProxy(String? proxyIP, String? proxyPort) {
String? localProxy = "PROXY $proxyIP:$proxyPort";
jdLog("localProxy=$localProxy");
(TDDioInstance().dio.httpClientAdapter as IOHttpClientAdapter)
.onHttpClientCreate = (client) {
client.badCertificateCallback = (cert, host, port) {
return true;
};
client.findProxy = (uri) {
// "PROXY 192.168.1.1:8888";
return localProxy;
};
return null;
};
}
}