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

속성 값

활성 ServicePoint 연결이 열린 상태로 유지되는 시간(밀리초)을 지정하는 Int32. 기본값은 -1이며 활성 ServicePoint 연결이 무기한으로 연결되도록 허용합니다. 요청을 처리한 후 ServicePoint 연결을 강제로 닫도록 하려면 이 속성을 0으로 설정합니다.

예외

set 연산에 지정된 값은 -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

설명

주의

WebRequest, HttpWebRequest, ServicePointWebClient 사용되지 않으므로 새 개발에 사용하면 안 됩니다. 대신 HttpClient 사용합니다.

이 속성을 사용하여 ServicePoint 개체의 활성 연결이 무기한으로 열려 있지 않도록 할 수 있습니다. 이 속성은 부하 분산 시나리오와 같이 연결을 정기적으로 삭제하고 다시 설정해야 하는 시나리오를 위한 것입니다.

기본적으로 KeepAlive 요청에 대해 true 경우 MaxIdleTime 속성은 비활성으로 인해 ServicePoint 연결을 닫기 위한 제한 시간을 설정합니다. ServicePoint 활성 연결이 있는 경우 MaxIdleTime 효과가 없으며 연결은 무기한으로 열려 있습니다.

ConnectionLeaseTimeout 속성이 -1이 아닌 값으로 설정되고 지정된 시간이 경과한 후 해당 요청에서 falseKeepAlive 설정하여 요청을 처리한 후 활성 ServicePoint 연결이 닫힙니다.

이 값을 설정하면 ServicePoint 개체에서 관리하는 모든 연결에 영향을 줍니다.

적용 대상

추가 정보