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

余生颓废 提交于 2020-01-16 05:50:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!