共用方式為


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,指定作用中 ServicePoint 連接保持開啟的毫秒數。 默認值為 -1,允許作用中 ServicePoint 連線無限期保持連線。 將此屬性設定為 0,以強制 ServicePoint 連線在服務要求之後關閉。

例外狀況

為設定作業指定的值是小於 -1 的負數。

範例

下列程式代碼範例會設定這個屬性的值。

#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.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

備註

謹慎

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

您可以使用這個屬性來確保 ServicePoint 物件的使用中連接不會無限期保持開啟。 此屬性適用於應該定期卸除和重新建立連線的案例,例如負載平衡案例。

根據預設,當要求 KeepAlivetrue 時,MaxIdleTime 屬性會設定因閑置而關閉 ServicePoint 連線的逾時。 如果 ServicePoint 有作用中的連線,MaxIdleTime 沒有任何作用,而且聯機會無限期地保持開啟。

ConnectionLeaseTimeout 屬性設定為 -1 以外的值,並在指定的時間經過之後,使用中 ServicePoint 連接會在服務要求之後關閉,方法是將 KeepAlive 設定為該要求中的 false

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

適用於

另請參閱