ServicePoint.ConnectionLeaseTimeout 属性

定义

获取或设置在多少毫秒之后关闭活动 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

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.

例外

为 Set 操作指定的值为小于 -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.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

注解

您可以使用此属性来确保 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.

默认情况下,当为 KeepAlive true 请求时, MaxIdleTime 属性将设置关闭由于不活动而关闭连接的超时时间 ServicePointBy 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 在处理请求后关闭活动连接,方法是 KeepAlive false 在该请求中将设置为。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.

设置此值将影响对象管理的所有连接 ServicePointSetting this value affects all connections managed by the ServicePoint object.

适用于

另请参阅