System.Net


.NET Framework 클래스 라이브러리
HttpListenerResponse 클래스

참고: 이 클래스는 .NET Framework 버전 2.0에서 새로 추가되었습니다.

HttpListener 개체에서 처리하고 있는 요청에 대한 응답을 나타냅니다.

네임스페이스: System.Net
어셈블리: System(system.dll)

구문

Visual Basic(선언)
Public NotInheritable Class HttpListenerResponse
    Implements IDisposable
Visual Basic(사용법)
Dim instance As HttpListenerResponse
C#
public sealed class HttpListenerResponse : IDisposable
C++
public ref class HttpListenerResponse sealed : IDisposable
J#
public final class HttpListenerResponse implements IDisposable
JScript
public final class HttpListenerResponse implements IDisposable
설명

클라이언트가 HttpListener 개체에서 처리하는 리소스를 요청할 때 응용 프로그램은 HttpListenerContext 개체에서 해당 요청 및 응답을 사용할 수 있습니다. 요청은 HttpListenerRequest 개체가 나타내며 HttpListenerContext.Request 속성에서 사용할 수 있습니다. 응답은 HttpListenerResponse 개체가 나타내며 HttpListenerContext.Response 속성에서 사용할 수 있습니다.

StatusCode, StatusDescriptionCookies 등의 다양한 속성을 설정하여 응답을 사용자 지정할 수 있습니다. 응답 데이터를 쓸 수 있는 대상 Stream 인스턴스를 가져오려면 HttpListenerResponse.OutputStream 속성을 사용합니다. 마지막으로, 클라이언트에 응답 데이터를 보내려면 Close 메서드를 호출합니다.

예제

다음 코드 예제에서는 클라이언트 요청에 응답하는 방법을 보여 줍니다.

C#
// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
    if (!HttpListener.IsSupported)
    {
        Console.WriteLine ("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
        return;
    }
    // URI prefixes are required,
    // for example "http://contoso.com:8080/index/".
    if (prefixes == null || prefixes.Length == 0)
      throw new ArgumentException("prefixes");
    
    // Create a listener.
    HttpListener listener = new HttpListener();
    // Add the prefixes.
    foreach (string s in prefixes)
    {
        listener.Prefixes.Add(s);
    }
    listener.Start();
    Console.WriteLine("Listening...");
    // Note: The GetContext method blocks while waiting for a request. 
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;
    // Obtain a response object.
    HttpListenerResponse response = context.Response;
    // Construct a response.
    string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    // Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length;
    System.IO.Stream output = response.OutputStream;
    output.Write(buffer,0,buffer.Length);
    // You must close the output stream.
    output.Close();
    listener.Stop();
}
상속 계층 구조

System.Object
  System.Net.HttpListenerResponse
스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.
플랫폼

Windows 98, Windows Server 2003, Windows XP Media Center Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0에서 지원
참고 항목

태그 :


Page view tracker