HttpListenerResponse.KeepAlive 속성

정의

서버에서 영구 연결을 요청하는지 여부를 나타내는 값을 가져오거나 설정합니다.

public:
 property bool KeepAlive { bool get(); void set(bool value); };
public bool KeepAlive { get; set; }
member this.KeepAlive : bool with get, set
Public Property KeepAlive As Boolean

속성 값

Boolean

서버에서 영구 연결을 요청하면 true이고, 그렇지 않으면 false입니다. 기본값은 true입니다.

예외

이 개체가 닫혀 있는 경우

예제

다음 코드 예제에서는이 속성의 값을 설정 하는 방법을 보여 줍니다.


// When the client is not authenticated, there is no Identity.
if (context.User == null)
{
    message.Append ("<HTML><BODY><p> Hello local user! </p></BODY></HTML>");
}
else
{
    // Get the requester's identity.
    System.Security.Principal.WindowsIdentity identity = WindowsIdentity.GetCurrent();
    // Construct the response body.
    message.AppendFormat ("<HTML><BODY><p> Hello {0}!<br/>",
        identity.Name);
    message.AppendFormat ("You were authenticated using {0}.</p>",
        identity.AuthenticationType);
    message.Append ("</BODY></HTML>");
}

// Configure the response.
HttpListenerResponse response = context.Response;

// Use the encoding from the response if one has been set.
// Otherwise, use UTF8.
System.Text.Encoding encoding = response.ContentEncoding;
if (encoding == null)
{
    encoding = System.Text.Encoding.UTF8;
    response.ContentEncoding = encoding;
}
byte[] buffer = encoding.GetBytes (message.ToString ());
response.ContentLength64 = buffer.Length;
response.StatusCode = (int) HttpStatusCode.OK;
response.StatusDescription = "OK";
response.ProtocolVersion = new Version ("1.1");
// Don't keep the TCP connection alive;
// We don't expect multiple requests from the same client.
response.KeepAlive = false;
// Write the response body.
System.IO.Stream stream = response.OutputStream;
stream.Write(buffer, 0, buffer.Length);

' When the client is not authenticated, there is no Identity.
If context.User Is Nothing Then
    message.Append("<HTML><BODY><p> Hello local user! </p></BODY></HTML>")
Else
    ' Get the requester's identity.
    Dim identity As System.Security.Principal.WindowsIdentity = WindowsIdentity.GetCurrent()
    ' Construct the response body.
    message.AppendFormat("<HTML><BODY><p> Hello {0}!<br/>", identity.Name)
    message.AppendFormat("You were authenticated using {0}.</p>", identity.AuthenticationType)
    message.Append("</BODY></HTML>")
End If

' Configure the response.
Dim response As HttpListenerResponse = context.Response

' Use the encoding from the response if one has been set.
' Otherwise, use UTF8.
Dim encoding As System.Text.Encoding = response.ContentEncoding
If encoding Is Nothing Then
    encoding = System.Text.Encoding.UTF8
    response.ContentEncoding = encoding
End If
Dim buffer() As Byte = encoding.GetBytes(message.ToString())
response.ContentLength64 = buffer.Length
response.StatusCode = CInt(HttpStatusCode.OK)
response.StatusDescription = "OK"
response.ProtocolVersion = New Version("1.1")
' Don't keep the TCP connection alive
' We don't expect multiple requests from the same client.
response.KeepAlive = False
' Write the response body.
Dim stream As System.IO.Stream = response.OutputStream
stream.Write(buffer, 0, buffer.Length)

설명

HTTP 클라이언트와 서버가 짧은 기간 동안 여러 번 데이터를 교환해야 하는 경우 영구 연결은 각 메시지에 대한 TCP 연결을 열고 닫는 데 필요한 오버헤드를 방지하여 통신 속도를 향상합니다. 영구 연결은 최신 웹 브라우저와 웹 서버 간의 통신에 널리 사용됩니다.

영구 연결은 RTF 편집기 웹 사이트https://www.rfc-editor.org/()에서 사용할 수 있는 HTTP/1.1 프로토콜 사양(RFC 2616)에 자세히 설명되어 있습니다.

적용 대상

추가 정보