当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Short domain name bypass and xss related knowledge
超越YOLO5-Face | YOLO-FaceV2正式开源Trick+学术点拉满
1349. Maximum number of students taking the exam Status Compression
【MySQL series】- Does LIKE query start with % will make the index invalid?
详细全面的postman接口测试实战教程
新来个技术总监,把DDD落地的那叫一个高级,服气
程序员失眠时的数羊列表 | 每日趣闻
安装oracle11的时候为什么会报这个问题
【机器学习】21天挑战赛学习笔记(二)
Oracle encapsulates restful interfaces into views
跨域解决方案
GCC: compile-time library path and runtime library path
直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计
Getting Started with Kubernetes Networking
source program in assembly language
深度学习:使用nanodet训练自己制作的数据集并测试模型,通俗易懂,适合小白
从一次数据库误操作开始了解MySQL日志【bin log、redo log、undo log】
缺陷检测(图像处理部分)
没有对象的程序员如何过七夕
C# const readonly static 关键字区别









