如何从 web.config 中的 system.net 部分读取默认代理设置的值



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

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

我最终通过配置管理器而不是通过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 类具有满足我需求的"启用"和"Proxy.ProxyAddress"属性。

使用以下 web.config 部分:

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

以下代码从 Web 配置返回代理信息:

Uri proxy = WebRequest.DefaultWebProxy.GetProxy(new System.Uri("http://www.google.com"));

最新更新