C# 如何从web.config中的system.net部分读取defaultProxy设置的值?

C# 如何从web.config中的system.net部分读取defaultProxy设置的值?,c#,proxy,web-config,C#,Proxy,Web Config,我试图在运行时读取默认代理设置的值,但似乎找不到任何方法。关于如何设置默认代理(例如),有很多相关的答案,但我正在寻找如何读取这些设置 这背后的原因是,我们有时会打开代理,以便使用Fiddler捕获服务器上的流量,我想创建一个故障保护,如果有人在关闭Fiddler后意外将其保留在这种状态,则会通知我。使用以下web.config部分: <defaultProxy useDefaultCredentials="true"> <proxy usesystemdefault="F

我试图在运行时读取默认代理设置的值,但似乎找不到任何方法。关于如何设置默认代理(例如),有很多相关的答案,但我正在寻找如何读取这些设置


这背后的原因是,我们有时会打开代理,以便使用Fiddler捕获服务器上的流量,我想创建一个故障保护,如果有人在关闭Fiddler后意外将其保留在这种状态,则会通知我。

使用以下web.config部分:

<defaultProxy useDefaultCredentials="true">
  <proxy usesystemdefault="False" proxyaddress="http://1.1.1.1" bypassonlocal="True" />
</defaultProxy>

使用以下web.config节:

<defaultProxy useDefaultCredentials="true">
  <proxy usesystemdefault="False" proxyaddress="http://1.1.1.1" bypassonlocal="True" />
</defaultProxy>

我最终通过Configuration manager而不是System.Net.WebProxy读取了这些值:

var proxy = System.Web.Configuration.WebConfigurationManager.GetSection("system.net/defaultProxy") as System.Net.Configuration.DefaultProxySection  
if (proxy != null) { /* Check Values Here */ }

DefalutProxySection类具有满足我需要的“Enabled”和“Proxy.ProxyAddress”属性。

我最终通过Configuration manager而不是System.Net.WebProxy读取了值:

var proxy = System.Web.Configuration.WebConfigurationManager.GetSection("system.net/defaultProxy") as System.Net.Configuration.DefaultProxySection  
if (proxy != null) { /* Check Values Here */ }

DefalutProxySection类具有满足我需要的“Enabled”和“Proxy.ProxyAddress”属性。

谢谢,Jamie。这几乎满足了我的需要。代理的所有属性似乎都可用,但不清楚如何检查代理是否已启用。另外,这个方法不会启动对URI的web请求吗?谢谢,Jamie。这几乎满足了我的需要。代理的所有属性似乎都可用,但不清楚如何检查代理是否已启用。另外,这个方法不会启动对URI的web请求吗?