HttpListenerResponse.StatusCode 속성

정의

클라이언트에 반환되는 HTTP 상태 코드를 가져오거나 설정합니다.Gets or sets the HTTP status code to be returned to the client.

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

속성 값

요청된 리소스에 대한 HTTP 상태 코드를 지정하는 Int32 값입니다.An Int32 value that specifies the HTTP status code for the requested resource. 기본값은 OK입니다. 이 값은 서버에서 클라이언트의 요청을 성공적으로 처리했고 요청된 리소스가 응답 본문에 포함되어 있음을 의미합니다.The default is OK, indicating that the server successfully processed the client's request and included the requested resource in the response body.

예외

이 개체가 닫혀 있는 경우This object is closed.

set 작업에 지정된 값이 유효하지 않은 경우.The value specified for a set operation is not valid. 유효한 값은 100부터 999까지입니다.Valid values are between 100 and 999 inclusive.

예제

다음 코드 예제에서는이 속성의 값을 설정 하는 방법을 보여 줍니다.The following code example demonstrates setting the value of this property.


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

설명

클라이언트는 서버에서 반환 된 상태 코드를 사용 하 여 진행 방법을 결정 합니다.Clients use the status code returned by the server to decide how to proceed. OK 값은 서버가 클라이언트의 요청을 성공적으로 처리 하 고 요청 된 리소스를 응답 본문에 포함 했음을 나타냅니다.A value of OK indicates that the server successfully processed the client's request and included the requested resource in the response body. 다른 일반적인 상태 코드에는 요청 된 리소스를 서버에서 찾을 수 없음을 나타내는 NotFound와 클라이언트의 캐시 된 리소스 복사본이 있으므로 요청 된 리소스를 응답 본문에 반환할 필요가 없음을 나타내는 NotModified있습니다. 최신 상태입니다.Other common status codes include NotFound, indicating that the requested resource was not found on the server, and NotModified, indicating that it was unnecessary to return the requested resource in the response body because the client's cached copy of the resource is up-to-date.

가능한 상태 코드의 전체 목록은 HttpStatusCode 열거를 참조 하세요.For a complete list of possible status codes, see the HttpStatusCode enumeration.

적용 대상

추가 정보