How can I read the values of the defaultProxy sett

2019-07-23 07:14发布

I am trying to read the values of my default proxy settings at runtime, but I can't seem to find any way of doing so. There are plenty of related answers on how to set a default proxy (e.g. How to pass credentials in defaultProxy config setting?), but I'm looking for how to read these settings.

The reason behind this is that we sometimes turn on the proxy so we can capture traffic on the server with Fiddler, and I want to create a fail-safe that notifies me if someone has accidentally left it in this state after closing Fiddler.

2条回答
看我几分像从前
2楼-- · 2019-07-23 07:34

With the following web.config section:

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

The following code returns the proxy information from web config:

Uri proxy = WebRequest.DefaultWebProxy.GetProxy(new System.Uri("http://www.google.com"));
查看更多
ら.Afraid
3楼-- · 2019-07-23 07:56

I ended up reading the values through the Configuration manager rather than through System.Net.WebProxy:

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

The DefalutProxySection class has properties for "Enabled" and "Proxy.ProxyAddress" that met my needs.

查看更多
登录 后发表回答