找到共享 2. 打开共享
遍及的做法是,选择一种要领,尝尝看;如果掉败了,不妨,再尝尝另外要领。不管怎么样,重要的是先去测验考试。
---富兰克林·罗斯福
前言前阵子有同学反馈Flutter中的http请求无法通过fiddler抓包,作者喜欢使用Charles抓包工具,于是抽时间写了个小demo测试了一下,结论是在手机上设置代办代理,Charles确实抓不到请求数据包。于是对该问题进行了分析:
确定使用的是http倡议的get请求,理论上http协议应该可以被Charles抓到包的,如果没有抓到包,那可能是没有走代办代理,于是乎通过将条记本连接的wifi断开测试了一下手机上APP倡议http请求,发明请求告成,证实确实没有走代办代理;
为什么http请求没有通过wifi走代办代理呢,因为之前安卓原生使用的一些http框架都是正常走代办代理的啊,那是不是有可能代码中有api要领可以设置请求不走代办代理,于是乎就研读了一下Flutter中http相关的源码,最终找到了答案。
http请求源码跟踪http.dart中的HttpClient是一个抽象类,成员要领的具体实此刻http_impl.dart中,http的get请求实现如下:
Future<HttpClientRequest> getUrl(Uri url) => _openUrl("get", url); Future<_HttpClientRequest> _openUrl(String method, Uri uri) { . . . // 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<String> 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 = "$option:1080"; } else { if (option.indexOf(":") == -1) option = "$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"; }
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/33243.html