設定網際網路應用程式

<system.Net> 元素 (網路設定) 組態元素包含應用程式的網路組態資訊。 使用 <system.Net> 元素 (網路設定) 元素,您可以設定 Proxy 伺服器、設定連線管理參數,以及在應用程式中包含自訂驗證和要求模組。

<defaultProxy> 元素 (網路設定) 元素定義 GlobalProxySelection 類別所傳回的 Proxy 伺服器。 任何未將其專屬 Proxy 屬性設定為特定值的 HttpWebRequest 都會使用預設 Proxy。 除了設定 Proxy 位址之外,您還可以建立將不會使用 Proxy 的伺服器位址清單,以及指出不應該將 Proxy 用於本機位址。

務必注意,系統的網際網路設定會結合組態設定,但後者優先使用。

下列範例會將預設 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> 元素 (網路設定) 元素,以設定可對特定伺服器或所有其他伺服器進行的持續連線數目。 下列範例會設定應用程式使用兩個與 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>  

另請參閱