Flutter中http请求抓包解决方案

.
.
.
// Check to see if a proxy server should be used for this connection.
var proxyConf = const _ProxyConfiguration.direct();
if (_findProxy != null) {
// TODO(sgjesse): Keep a map of these as normally only a few
// configuration strings will be used.
try {
proxyConf = new _ProxyConfiguration(_findProxy(uri));
} catch (error, stackTrace) {
return new Future.error(error, stackTrace);
}
}
return _getConnection(uri.host, port, proxyConf, isSecure)
.then((_ConnectionInfo info) {
.
.
.
});
}

首先,我们可以发现方法中有一行注释// Check to see if a proxy server should be used for this connection.,意思是“检查是否应该使用代理服务器进行此连接”;

然后,有一个proxyConf对象初始化和根据_findProxy来创建新的proxyConf对象的语句,然后通过_getConnection(uri.host, port, proxyConf, isSecure)来创建连接,_getConnection的源码如下:

Future<_ConnectionInfo> _getConnection(String uriHost, int uriPort,
_ProxyConfiguration proxyConf, bool isSecure) {
Iterator<_Proxy> proxies = proxyConf.proxies.iterator;

Future<_ConnectionInfo> connect(error) {
if (!proxies.moveNext()) return new Future.error(error);
_Proxy proxy = proxies.current;
String host = proxy.isDirect ? uriHost : proxy.host;
int port = proxy.isDirect ? uriPort : proxy.port;
return _getConnectionTarget(host, port, isSecure)
.connect(uriHost, uriPort, proxy, this)
// On error, continue with next proxy.
.catchError(connect);
}

return connect(new HttpException(“No proxies given”));
}

从代码中我们可以看到根据代理配置信息来将请求的host和port进行重置,然后创建真实的连接。

跟踪以上源码我们发现dart中http请求是否走代理是需要配置的,而_findProxy变量和配置的代理信息有关。

http__impl.dart文件中的_HttpClient类中定义了_findProxy的默认值

Function _findProxy = HttpClient.findProxyFromEnvironment;

HttpClient类中findProxyFromEnvironment方法的实现

static String findProxyFromEnvironment(Uri url,
{Map<String, String> environment}) {
HttpOverrides overrides = HttpOverrides.current;
if (overrides == null) {
return _HttpClient._findProxyFromEnvironment(url, environment);
}
return overrides.findProxyFromEnvironment(url, environment);
}

_HttpClient类中_findProxyFromEnvironment方法的实现

static String _findProxyFromEnvironment(
Uri url, Map<String, String> environment) {
checkNoProxy(String option) {
if (option == null) return null;
Iterator names = option.split(“,”).map((s) => s.trim()).iterator;
while (names.moveNext()) {
var name = names.current;
if ((name.startsWith(“[”) &&
name.endsWith(“]”) &&
“[${url.host}]” == name) ||
(name.isNotEmpty && url.host.endsWith(name))) {
return “DIRECT”;
}
}
return null;
}

checkProxy(String option) {
if (option == null) return null;
option = option.trim();
if (option.isEmpty) return null;
int pos = option.indexOf(“😕/”);
if (pos >= 0) {
option = option.substring(pos + 3);
}
pos = option.indexOf(“/”);
if (pos >= 0) {
option = option.substring(0, pos);
}
// Add default port if no port configured.
if (option.indexOf(“[”) == 0) {
var pos = option.lastIndexOf(“:”);
if (option.indexOf(“]”) > pos) option = “KaTeX parse error: Expected 'EOF', got '}' at position 15: option:1080"; }̲ else { if (opt…option:1080”;
}
return “PROXY $option”;
}

// Default to using the process current environment.
if (environment == null) environment = _platformEnvironmentCache;

String proxyCfg;

String noProxy = environment[“no_proxy”];
if (noProxy == null) noProxy = environment[“NO_PROXY”];
if ((proxyCfg = checkNoProxy(noProxy)) != null) {
return proxyCfg;
}

if (url.scheme == “http”) {
String proxy = environment[“http_proxy”];
if (proxy == null) proxy = environment[“HTTP_PROXY”];
if ((proxyCfg = checkProxy(proxy)) != null) {
return proxyCfg;
}
} else if (url.scheme == “https”) {
String proxy = environment[“https_proxy”];
if (proxy == null) proxy = environment[“HTTPS_PROXY”];
if ((proxyCfg = checkProxy(proxy)) != null) {
return proxyCfg;
}
}
return “DIRECT”;
}

从以上代码中可以发现代理配置从environment中读取,设置代理时必须指定http_proxyhttps_proxy等。而从_openUrl方法实现中proxyConf = new _ProxyConfiguration(_findProxy(uri));得出默认情况下environment是为空的,所以要想在Flutter的http请求中使用代理,则要指定相应的代理配置,即设置httpClient.findProxy的值。

示例代码:

_getHttpData() async {
var httpClient = new HttpClient();
httpClient.findProxy = (url) {
return HttpClient.findProxyFromEnvironment(url, environment: {“http_proxy”: ‘http://192.168.124.7:8888’,});
};
var uri =
new Uri.http(‘t.weather.sojson.com’, ‘/api/weather/city/101210101’);
var request = await httpClient.getUrl(uri);
var response = await request.close();
if (response.statusCode == 200) {
print(‘请求成功’);
var responseBody = await response.transform(Utf8Decoder()).join();
print(‘responseBody = $responseBody’);
} else {
print(‘请求失败’);
}
}

以上代码设置后即可使用Fiddler或Charles抓包了。

注:

  • 代码中已设置代理,手机wifi不再需要进行代理设置;

尾声

最后,我再重复一次,如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

最后想要拿高薪实现技术提升薪水得到质的飞跃。最快捷的方式,就是有人可以带着你一起分析,这样学习起来最为高效,所以为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。

当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的。

进阶学习视频

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题 (含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)


《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
roid扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

[外链图片转存中…(img-t1Oloe6c-1714963668868)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值