HttpListenerResponse.StatusCode 속성

정의

클라이언트에 반환되는 HTTP 상태 코드를 가져오거나 설정합니다.

public:
 property int StatusCode { int get(); void set(int value); };
public int StatusCode { get; set; }
member this.StatusCode : int with get, set
Public Property StatusCode As Integer

속성 값

Int32

요청된 리소스에 대한 HTTP 상태 코드를 지정하는 Int32 값입니다. 기본값은 OK입니다. 이 값은 서버에서 클라이언트의 요청을 성공적으로 처리했고 요청된 리소스가 응답 본문에 포함되어 있음을 의미합니다.

예외

이 개체가 닫혀 있는 경우

set 작업에 지정된 값이 유효하지 않은 경우. 유효한 값은 100부터 999까지입니다.

예제

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


// 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)

설명

클라이언트는 서버에서 반환된 상태 코드를 사용하여 진행 방법을 결정합니다. 값 OK 은 서버가 클라이언트의 요청을 성공적으로 처리하고 요청된 리소스를 응답 본문에 포함했음을 나타냅니다. 다른 일반적인 상태 코드에는 NotFound서버에서 요청된 리소스를 찾을 수 없음을 나타내는 코드와 NotModified클라이언트의 캐시된 리소스 복사본이 최신 상태이므로 응답 본문에서 요청된 리소스를 반환할 필요가 없음을 나타내는 코드가 포함됩니다.

가능한 상태 코드의 전체 목록은 열거형을 HttpStatusCode 참조하세요.

적용 대상

추가 정보