一.关于Charles的下载和配置可以参考Charles https 抓包
二.关于flutter dio设置代理
简单说下几种方式:
1.一种是运行的时候直接设置
首先在dio初始化后直接设置如下
void checkForCharlesProxy(Dio dio) {
const charlesIp =
String.fromEnvironment('CHARLES_PROXY_IP', defaultValue: null);
if (charlesIp == null) return;
debugPrint('#CharlesProxyEnabled');
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(client) {
client.findProxy = (uri) => "PROXY $charlesIp:8888;";
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
};
}
然后用命令行运行,ip换成自己的就可以了,例如
flutter run --dart-define=CHARLES_PROXY_IP=192.168.0.52
以上方法参照https://medium.com/netguru/how-to-connect-charles-proxy-with-dio-4443af7bbaa8,开发自己用挺好,但不方便给测试使用。
2.另外一种是利用第三方插件http_proxy
自动获取手机的代理,然后dio创建后设置
代码如下:
import 'package:http_proxy/http_proxy.dart';
void checkForCharlesProxy(Dio dio) async {
HttpProxy httpProxy = await HttpProxy.createHttpProxy();
var proxyHost = httpProxy.host;
var proxyPort = httpProxy.port;
if (proxyHost == null) return;
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
(client) {
client.findProxy = (uri) => "PROXY $proxyHost:$proxyPort;";
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
};
}
目前测试过几个手机,都能成功抓包,这样给测试打的包也可以抓包了