简体   繁体   中英

How can I read the values of the defaultProxy settings from the system.net section in a web.config?

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 (eg 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.

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.

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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM