【问题标题】:How can I read the values of the defaultProxy settings from the system.net section in a web.config?如何从 web.config 的 system.net 部分读取 defaultProxy 设置的值?
【发布时间】:2014-02-21 16:47:45
【问题描述】:

我试图在运行时读取我的默认代理设置的值,但我似乎找不到任何这样做的方法。关于如何设置默认代理(例如How to pass credentials in defaultProxy config setting?)有很多相关答案,但我正在寻找如何阅读这些设置。

这背后的原因是我们有时会打开代理,这样我们就可以使用 Fiddler 捕获服务器上的流量,并且我想创建一个故障保护功能,如果有人在关闭 Fiddler 后不小心将其留在此状态,我会通知我.

【问题讨论】:

    标签: c# proxy web-config


    【解决方案1】:

    我最终通过配置管理器而不是通过 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”属性。

    【讨论】:

      【解决方案2】:

      使用以下 web.config 部分:

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

      以下代码从 web config 返回代理信息:

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

      【讨论】:

      • 谢谢,杰米。这几乎满足了我的需要。代理的所有属性似乎都可用,但不清楚如何检查代理是否启用。另外,这个方法不会向 URI 发起 Web 请求吗?
      猜你喜欢
      • 2010-11-28
      • 2012-11-07
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      相关资源
      最近更新 更多