![](/img/trans.png)
[英]How to read system.net/mailSettings/smtp from Web.config
[英]How can I read the values of the defaultProxy settings from the system.net section in a web.config?
我試圖在運行時讀取默認代理設置的值,但似乎找不到任何方法。 關於如何設置默認代理(例如, 如何在defaultProxy配置設置中傳遞憑據? )有很多相關的答案,但是我正在尋找如何閱讀這些設置的方法。
其背后的原因是,我們有時會打開代理,以便可以使用Fiddler捕獲服務器上的流量,並且我想創建一個故障保護,如果有人在關閉Fiddler后意外將其保留在該狀態下,則會通知我。
我最終通過配置管理器而不是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類具有滿足我需要的“ Enabled”和“ Proxy.ProxyAddress”屬性。
使用以下web.config部分:
<defaultProxy useDefaultCredentials="true">
<proxy usesystemdefault="False" proxyaddress="http://1.1.1.1" bypassonlocal="True" />
</defaultProxy>
以下代碼從Web配置返回代理信息:
Uri proxy = WebRequest.DefaultWebProxy.GetProxy(new System.Uri("http://www.google.com"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.