語言

ServicePoint.ConnectionLeaseTimeout 屬性

定義

取得或設定在幾毫秒內關閉一個活躍 ServicePoint 連線。

public:
 property int ConnectionLeaseTimeout { int get(); void set(int value); };
public int ConnectionLeaseTimeout { get; set; }
member this.ConnectionLeaseTimeout : int with get, set
Public Property ConnectionLeaseTimeout As Integer

屬性值

Int32 A 表示一個活躍ServicePoint連線保持開啟的毫秒數。 預設值為 -1,允許活躍 ServicePoint 連線無限期保持連線。 將此屬性設為 0,以強制 ServicePoint 連線在服務請求後關閉。

例外狀況

設定的值為小於 -1 的負數。

範例

以下程式碼範例設定此屬性的值。

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace Examples.System.Net
{
    public class ServicePointExample
    {
        // Pass in the name of the Web page to retrieve.
        public static void Main(string[] args)
        {
            string page;
            if (args == null || args.Length == 0 || args[0].Length == 0)
            {
                page = "http://www.contoso.com/default.html";
            }
            else
            {
                page = args[0];
            }
            // Create the request.
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(page);
            // Get the service point that handles the request's socket connection.
            ServicePoint point = request.ServicePoint;
            // Set the receive buffer size on the underlying socket.
            point.ReceiveBufferSize = 2048;
            // Set the connection lease timeout to infinite.
            point.ConnectionLeaseTimeout = Timeout.Infinite;
            // Send the request.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader s = new StreamReader(responseStream);
            // Display the response.
            Console.WriteLine(s.ReadToEnd());
            s.Close();
            responseStream.Close();
            response.Close();
        }
    }
}
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading

Public Class ServicePointExample

    ' Pass in the name of the Web page to retrieve.
    Public Shared Sub Main(ByVal args() As String)
        Dim page As String
        If args Is Nothing OrElse args.Length = 0 OrElse args(0).Length = 0 Then
            page = "http://www.contoso.com/default.html"
        Else
            page = args(0)
        End If

        Dim request As HttpWebRequest = CType(WebRequest.Create(page), HttpWebRequest)
        ' Get the service point that handles the request's socket connection.
        Dim point As ServicePoint = request.ServicePoint
        ' Set the receive buffer size on the underlying socket.
        point.ReceiveBufferSize = 2048
        ' Set the connection lease timeout to infinite.
        point.ConnectionLeaseTimeout = Timeout.Infinite
        ' Send the request.
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        Dim responseStream As Stream = response.GetResponseStream()
        Dim s As New StreamReader(responseStream)
        ' Display the response.
        Console.WriteLine(s.ReadToEnd())
        responseStream.Close()
        response.Close()
    End Sub
End Class

備註

注意事項

WebRequest、、HttpWebRequest、ServicePointWebClient、和 都已過時,你不應該用它們來做新開發。 請改用 HttpClient。

你可以利用這個特性來確保 ServicePoint 物件的主動連線不會無限期地保持開啟狀態。 此特性適用於需要定期斷開與重建連線的情況,例如負載平衡情境。

預設情況下,當 KeepAlive 是 true 請求時,屬性 MaxIdleTime 會設定因非活動而關閉 ServicePoint 連線的逾時。 如果有 ServicePoint 活躍連線, MaxIdleTime 則無影響且連線會無限期保持開啟。

當屬性ConnectionLeaseTimeout設定為非 -1 值,且在指定時間過後,服務請求後,將啟用ServicePoint連接關閉KeepAlivefalse。

設定此值會影響物件管理 ServicePoint 的所有連線。

Note

自 .NET 9 起,此屬性映射至 SocketsHttpHandler.PooledConnectionLifetime。 然而,處理器並不會在請求間重複使用,因此沒有實質影響。

適用於

另請參閱