設定網際網路應用程式
System.Net > 元素 (Network 設定) configuration 元素包含應用程式的網路設定資訊。 使用 system.Net > 元素 (Network 設定) 元素,您可以設定 proxy 伺服器、設定連接管理參數,以及在應用程式中包含自訂驗證和要求模組。
DefaultProxy > 元素 (Network 設定) 元素會定義類別所傳回 GlobalProxySelection
的 proxy 伺服器。 任何未將其專屬 Proxy 屬性設定為特定值的 HttpWebRequest 都會使用預設 Proxy。 除了設定 Proxy 位址之外,您還可以建立將不會使用 Proxy 的伺服器位址清單,以及指出不應該將 Proxy 用於本機位址。
請務必注意,Microsoft Internet Explorer 設定是與組態設定一起使用,但優先使用後者。
下列範例會將預設 Proxy 伺服器位址設定為 http://proxyserver
、指出不應該將 Proxy 用作本機位址,以及指定對 contoso.com 網域所在伺服器的所有要求都應該略過 Proxy。
<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 > 元素 (Network 設定) 元素來設定可對特定伺服器或所有其他伺服器進行的持續連線數目。 下列範例會設定應用程式使用兩個與 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 > 元素設定 (Network 設定) 元素。 自訂驗證模組必須實作 IAuthenticationModule 介面。
下列範例設定自訂驗證模組。
<configuration>
<system.net>
<authenticationModules>
<add type="MyAuthModule, MyAuthModule.dll" />
</authenticationModules>
</system.net>
</configuration>
您可以使用 webRequestModules > 元素 (Network 設定) 元素,將應用程式設定為使用自訂的通訊協定特定模組來要求來自網際網路資源的資訊。 指定的模組必須實作 IWebRequestCreate 介面。 您可以在組態檔中指定自訂模組來覆寫預設 HTTP、HTTPS 和檔案要求模組,如下列範例所示。
<configuration>
<system.net>
<webRequestModules>
<add
prefix="HTTP"
type = "MyHttpRequest.dll, MyHttpRequestCreator"
/>
</webRequestModules>
</system.net>
</configuration>
另請參閱
- 以 .NET Framework 進行網路程式設計
- 網路設定架構
- (Network 設定) 的 system.Net > 元素