当前位置:网站首页>Dotnet 6 Why does the network request not follow the change of the system network proxy and dynamically switch the proxy?
Dotnet 6 Why does the network request not follow the change of the system network proxy and dynamically switch the proxy?
2022-08-05 01:52:00 【dotNET cross-platform】
本文记录在 dotnet 6 network and in .NET Framework changes in behavior.在 dotnet 6 下,The default network request is made when the system network proxy is changed,Agents are not dynamically switched.For example after the application is running for network communication,打开 Fiddler 抓包,此时将会发现 Fiddler 抓不到包,Only caught after restarting the app.Or open Fiddler 抓包,然后退出 Fiddler After that, the application is disconnected
This behavior is because Fiddler One of the principles of packet capture is to set the system's local network proxy,而由于 dotnet 6 下,Apps do not dynamically switch proxies,If after the application starts for network communication,再打开 Fiddler 抓包,在 Fiddler 打开之后,Will modify the system's native network proxy,但是 dotnet 6 The application does not go because it does not dynamically switch the proxy by default Fiddler 的代理,因此 Fiddler 抓不到包.同理,在开着 Fiddler 抓包之后,退出了 Fiddler The network proxy of this machine will be modified,但是由于 dotnet 6 The app does not dynamically switch proxies by default,在 Fiddler After modifying the native network proxy,依然 dotnet 6 of apps that are still in use are closed Fiddler The network proxy is disconnected from the network
核心原因是在 dotnet 6 Changed the behavior of dynamic switching of network proxies below.In fact, archaeology found this behavior .NET Core 2.0 That is, by default, it does not support to automatically follow the system proxy switch and modify the proxy
在 .NET Framework 的 4.0 开始,By monitoring the registry HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections
的变更,Proxy for refreshing network requests after changes.详细请看 https://referencesource.microsoft.com/#System/net/System/Net/_AutoWebProxyScriptEngine.cs,395
在 .NET Core 下,The network proxy is acquired only once,The obtained proxy no longer listens for registry changes,It didn't refresh again.This issue has been reported to the official,详细请看 https://github.com/dotnet/runtime/issues/46910
在 .NET Core will be obtained for the first time HttpClient.DefaultProxy 时进行初始化,值得一提的是在 .NET Core 调用的 WebRequest.GetSystemWebProxy 方法底层也是调用 HttpClient.DefaultProxy 属性
public static IWebProxy GetSystemWebProxy() => HttpClient.DefaultProxy;
以上的 GetSystemWebProxy 实现请看 Make WebRequest.GetSystemWebProxy() return a working proxy by stephentoub · Pull Request #41692 · dotnet/corefx
在 HttpClient.DefaultProxy 里面,将会调用到 SystemProxyInfo.cs 的 ConstructSystemProxy The method gets the proxy of the corresponding platform.这个 ConstructSystemProxy 在 OSX 和 Unix 和 Windows have their own implementations
在 Windows 实现如下
public static IWebProxy ConstructSystemProxy()
{
if (!HttpEnvironmentProxy.TryCreate(out IWebProxy? proxy))
{
HttpWindowsProxy.TryCreate(out proxy);
}
return proxy ?? new HttpNoProxy();
}
在 HttpEnvironmentProxy 里面,Will try to get the proxy's configuration via environment variables,也就是说 dotnet 6 The application supports setting proxies via environment variables,This makes debugging easier.The obtained environment variables are respectively ALL_PROXY
和 HTTP_PROXY
和 HTTPS_PROXY
these convention variables
如上面代码,If the environment variables cannot be obtained,那么就进入 HttpWindowsProxy 的代码.在 WinInetProxyHelper The proxy for the system will be read
如上面代码,可以看到,实际上在 HttpClient.DefaultProxy It will only be obtained once,No refresh through registry changes again
That's why network requests don't follow native network proxy changes
One solution is to copy dotnet runtime The configuration method of the reading system,Plus listening for registry changes to refresh the configuration,In this way, it can dynamically follow the changes of the system agent.I copied the code,wrote a version,使用方法是
var dynamicHttpWindowsProxy = new DynamicHttpWindowsProxy();HttpClient.DefaultProxy = dynamicHttpWindowsProxy;
The implementation of the code is placedgithub 和 gitee 欢迎访问
The source code can be obtained as follows,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,code is available
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin 8c64e9676c4205e55fad227a86d5d8d95a5ebe91
以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源.请在命令行继续输入以下代码
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin 8c64e9676c4205e55fad227a86d5d8d95a5ebe91
获取代码之后,进入 NilerlanaihikaWhurreeberhalur 文件夹,具体实现放在 Proxy
文件里面,在 Program.cs Contains test logic,You can keep trying to access Baidu.Can be tested in use HttpClient.DefaultProxy = dynamicHttpWindowsProxy;
时,切换 Fiddler 代理配置,和不使用 DynamicHttpWindowsProxy Toggle configured behavior
The above code is basically from dotnet runtime 里面抄的,It can be used with confidence in formal projects.Listening for registry changes is from https://www.codeproject.com/Articles/4502/RegistryMonitor-a-NET-wrapper-class-for-RegNotifyC 抄的,This is a relatively old and stable code,It just needs to open one more thread to monitor the registry.That's why in the example code,Will delay to start listening registry
参考文档:
c# - Default proxy in .net core 2.0 - Stack Overflow
AutoWebProxyScriptEngine.cs
Make WebRequest.GetSystemWebProxy() return a working proxy by stephentoub · Pull Request #41692 · dotnet/corefx
WinHttpWebProxyDataBuilder.cs
runtime/HttpConnectionPoolManager.cs at 1d9e50cb4735df46d3de0cee5791e97295eaf588 · dotnet/runtime
HttpClient.DefaultProxy should respect IE proxy changes · Issue #46910 · dotnet/runtime
how to set default proxy with .NET core 3.1 for HTTP client for any request? - Stack Overflow
How to change Global Windows Proxy using C# .NET with
Immediate Effect
- Stack Overflow
边栏推荐
猜你喜欢
VOC格式数据集转COCO格式数据集
Gartner Hype Cycle:超融合技术将在2年内到达“生产力成熟期”
Transfer Learning - Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
ExcelPatternTool: Excel table-database mutual import tool
“嘀哩哩,等灯等灯”,工厂安全生产的提示音
行业现状?互联网公司为什么宁愿花20k招人,也不愿涨薪留住老员工~
安装oracle11的时候为什么会报这个问题
sqlite--nested exception is org.apache.ibatis.exceptions.PersistenceException:
Leetcode brushing questions - 22. Bracket generation
JWT简单介绍
随机推荐
KingbaseES V8 GIS数据迁移方案(2. Kingbase GIS能力介绍)
Log an error encountered when compiling google gn "I could not find a ".gn" file ..."
MySQL3
【TA-霜狼_may-《百人计划》】图形4.3 实时阴影介绍
【Endnote】Word插入自定义形式的Endnote文献格式
【机器学习】21天挑战赛学习笔记(二)
How to create an rpm package
《.NET物联网从零开始》系列
Residential water problems
ExcelPatternTool: Excel表格-数据库互导工具
【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗
一文看懂推荐系统:召回06:双塔模型——模型结构、训练方法,召回模型是后期融合特征,排序模型是前期融合特征
第十一章 开关级建模
day14--postman接口测试
Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 1)
1349. Maximum number of students taking the exam Status Compression
基于OpenVINO工具套件简单实现YOLOv7预训练模型的部署
10年测试经验,在35岁的生理年龄面前,一文不值
原生js实现多选框全部选中和取消效果
使用OpenVINO实现飞桨版PGNet推理程序