当前位置:网站首页>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
边栏推荐
- ORA-00604 ORA-02429
- Gartner Hype Cycle:超融合技术将在2年内到达“生产力成熟期”
- Methods commonly used interface automation test framework postman tests
- Interview summary: Why do interviewers in large factories always ask about the underlying principles of Framework?
- 自定义线程池
- IJCAI2022 | DictBert:采用对比学习的字典描述知识增强的预训练语言模型
- EBS uses virtual columns and hint hints to optimize sql case
- A new technical director, who calls DDD a senior, is convinced
- 2022 EdgeX中国挑战赛8月3日即将盛大开幕
- MySQL3
猜你喜欢

第十一章 开关级建模

Utilities 
Leetcode刷题——22. 括号生成

Use of pytorch: Convolutional Neural Network Module

iNFTnews | What can NFTs bring to the sports industry and fans?

The use of pytorch: temperature prediction using neural networks

A new technical director, who calls DDD a senior, is convinced

Getting Started with Kubernetes Networking

Transfer Learning - Distant Domain Transfer Learning

软件测试技术之最有效的七大性能测试技术
随机推荐
.Net C# 控制台 使用 Win32 API 创建一个窗口
【Endnote】Word插入自定义形式的Endnote文献格式
1349. Maximum number of students taking the exam Status Compression
pytorch的使用:卷积神经网络模块
开篇-开启全新的.NET现代应用开发体验
手把手基于YOLOv5定制实现FacePose之《YOLO结构解读、YOLO数据格式转换、YOLO过程修改》
A new technical director, who calls DDD a senior, is convinced
1349. 参加考试的最大学生数 状态压缩
Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec
释放技术创新引擎,英特尔携手生态合作伙伴推动智慧零售蓬勃发展
直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
Leetcode刷题——22. 括号生成
如何看待自己的羞愧感
[Word] #() error occurs after Word formula is exported to PDF
4. PCIe interface timing
如何发现一个有价值的 GameFi?
【MySQL series】- Does LIKE query start with % will make the index invalid?
迁移学习——Distant Domain Transfer Learning
PHP技能评测
[Endnote] Word inserts a custom form of Endnote document format