ServicePoint.ConnectionLeaseTimeout ServicePoint.ConnectionLeaseTimeout ServicePoint.ConnectionLeaseTimeout ServicePoint.ConnectionLeaseTimeout Property

定義

取得或設定現用 ServicePoint 連接關閉前的毫秒數。Gets or sets the number of milliseconds after which an active ServicePoint connection is closed.

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,指定現用 ServicePoint 連接維持開啟的毫秒數。A Int32 that specifies the number of milliseconds that an active ServicePoint connection remains open. 預設值為 -1,這允許現用 ServicePoint 連接無限期保持連接狀態。The default is -1, which allows an active ServicePoint connection to stay connected indefinitely. 將這個屬性設為 0,可以強制 ServicePoint 連接在服務要求之後關閉。Set this property to 0 to force ServicePoint connections to close after servicing a request.

例外狀況

為設定作業指定的值是小於 -1 的負數。The value specified for a set operation is a negative number less than -1.

範例

下列程式碼範例會設定這個屬性的值。The following code example sets the value of this property.

#using <System.dll>

using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::IO;
using namespace System::Threading;

namespace SystemNetExamples
{
    public ref class ServicePointExample
    {
    public:

        // Pass in the name of the Web page to retrieve.    
        static void PrintResponse(String^ page)
        {

            // Create the request.
            HttpWebRequest^ request;
            Uri^ uri;
			
 

            try
            {
                uri = gcnew Uri(page);
            }
            catch (UriFormatException^ ex) 
            {
                Console::WriteLine(ex->Message);
            }

            request = (HttpWebRequest^) WebRequest::Create(uri); 

            // 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^ streamReader = 
                gcnew StreamReader(responseStream);
            try
            {
                // Display the response.
                Console::WriteLine(streamReader->ReadToEnd());
                responseStream->Close();
                response->Close();
            }
            finally
            {
                streamReader->Close();
            }
        }
    };
}

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
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 'Main
End Class

備註

您可以使用這個屬性,以確保ServicePoint物件的作用中的連線不會持續開啟無限期。You can use this property to ensure that a ServicePoint object's active connections do not remain open indefinitely. 這個屬性適用於案例應該卸除並重新建立定期,例如負載平衡案例連線。This property is intended for scenarios where connections should be dropped and reestablished periodically, such as load balancing scenarios.

根據預設,當KeepAlivetrue要求,MaxIdleTime屬性會設定要關閉逾時ServicePoint閒置的連線。By default, when KeepAlive is true for a request, the MaxIdleTime property sets the time-out for closing ServicePoint connections due to inactivity. 如果ServicePoint有使用中連接時,MaxIdleTime無效且無限期地保持開啟的連線。If the ServicePoint has active connections, MaxIdleTime has no effect and the connections remain open indefinitely.

ConnectionLeaseTimeout屬性設定為-1,且之後指定的時間過後,使用中以外的值ServicePoint藉由設定服務要求之後關閉連接KeepAlivefalse該要求中。When the ConnectionLeaseTimeout property is set to a value other than -1, and after the specified time elapses, an active ServicePoint connection is closed after servicing a request by setting KeepAlive to false in that request.

設定這個值會影響所管理的所有連線ServicePoint物件。Setting this value affects all connections managed by the ServicePoint object.

適用於

另請參閱