다음을 통해 공유


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입니다. 기본값은 활성 ServicePoint 연결이 무기한으로 연결되도록 하는 -1입니다. 요청을 처리한 후 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

설명

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

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

속성이 ConnectionLeaseTimeout -1 이외의 값으로 설정되고 지정된 시간이 경과한 후 해당 요청에서 을 로 설정 KeepAlivefalse 하여 요청을 서비스한 후 활성 ServicePoint 연결이 닫힙니다.

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

적용 대상

추가 정보