<system.Net>元素(網路設定)配置元素包含應用程式的網路設定資訊。 透過 <system.Net> Element(網路設定) 元素,你可以設定代理伺服器、設定連線管理參數,並在應用程式中加入自訂的認證與請求模組。
該 <defaultProxy> 元素 定義了由 GlobalProxySelection 類別回傳的代理伺服器。 任何 HttpWebRequest 沒有 Proxy 自己屬性設定為特定值的,都會使用預設代理伺服器。 除了設定代理位址外,你還可以建立一個不會使用代理伺服器的伺服器位址清單,並且可以指定代理不應用於本地位址。
值得注意的是,系統的網際網路設定會與設定設定合併使用,後者優先。
以下範例將預設代理伺服器位址設為 http://proxyserver,表示代理不應用於本地位址,並規定所有發送給位於 contoso.com 域伺服器的請求都必須繞過代理伺服器。
<configuration>
<system.net>
<defaultProxy>
<proxy
usesystemdefault = "false"
proxyaddress = "http://proxyserver:80"
bypassonlocal = "true"
/>
<bypasslist>
<add address="http://[a-z]+\.contoso\.com/" />
</bypasslist>
</defaultProxy>
</system.net>
</configuration>
使用 <connectionManagement> 元素(網路設定) 來設定可與特定伺服器或所有其他伺服器建立的持久連線數量。 以下範例將應用程式設定為使用兩個持久連線到伺服器 www.contoso.com、四個持久連線到擁有 IP 位址 192.168.1.2 的伺服器,以及一個持續連線到所有其他伺服器。
<configuration>
<system.net>
<connectionManagement>
<add address="http://www.contoso.com" maxconnection="2" />
<add address="192.168.1.2" maxconnection="4" />
<add address="*" maxconnection="1" />
</connectionManagement>
</system.net>
</configuration>
自訂認證模組則以 <authenticationModules> 元素(網路設定) 元素配置。 必須由自訂認證模組實作該 IAuthenticationModule 介面。
以下範例配置一個自訂的認證模組。
<configuration>
<system.net>
<authenticationModules>
<add type="MyAuthModule, MyAuthModule.dll" />
</authenticationModules>
</system.net>
</configuration>
你可以使用 <webRequestModules> 元素(網路設定) 元素來設定應用程式,使用自訂協定專用模組來向網際網路資源請求資訊。 指定的模組必須實作該 IWebRequestCreate 介面。 你可以在設定檔中指定自訂模組,如以下範例,覆蓋預設的 HTTP、HTTPS 和檔案請求模組。
<configuration>
<system.net>
<webRequestModules>
<add
prefix="HTTP"
type = "MyHttpRequest.dll, MyHttpRequestCreator"
/>
</webRequestModules>
</system.net>
</configuration>