问题
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.
回答1:
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.
回答2:
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"));
来源:https://stackoverflow.com/questions/21940241/how-can-i-read-the-values-of-the-defaultproxy-settings-from-the-system-net-secti