HttpListener.GetContext 메서드

정의

들어오는 요청을 기다리고 요청을 받으면 반환됩니다.

public:
 System::Net::HttpListenerContext ^ GetContext();
public System.Net.HttpListenerContext GetContext ();
member this.GetContext : unit -> System.Net.HttpListenerContext
Public Function GetContext () As HttpListenerContext

반환

클라이언트 요청을 나타내는 HttpListenerContext 개체입니다.

예외

Win32 함수 호출이 실패한 경우. 예외의 ErrorCode 속성을 검토하여 예외의 원인을 확인할 수 있습니다.

이 개체가 시작되지 않았거나 현재 중지된 경우

또는

HttpListener에 응답을 보낼 대상 URI(Uniform Resource Identifier) 접두사가 없는 경우.

이 개체가 닫혀 있는 경우

예제

다음 코드 예제에서는 이 메서드를 호출하는 방법을 보여 줍니다.

// 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();
}
Public Shared Sub SimpleListenerExample(prefixes As String())
    If Not HttpListener.IsSupported Then
        Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.")
        Return
    End If
    ' URI prefixes are required,
    ' for example "http://contoso.com:8080/index/".
    If prefixes Is Nothing Or prefixes.Length = 0 Then
        Throw New ArgumentException("prefixes")
    End If

    ' Create a listener
    Dim listener = New HttpListener()

    For Each s As String In prefixes
        listener.Prefixes.Add(s)
    Next
    listener.Start()
    Console.WriteLine("Listening...")
    ' Note: The GetContext method blocks while waiting for a request.
    Dim context As HttpListenerContext = listener.GetContext()
    Console.WriteLine("Listening...")
    ' Obtain a response object
    Dim request As HttpListenerRequest = context.Request
    ' Construct a response.
    Dim response As HttpListenerResponse = context.Response
    Dim responseString As String = "<HTML><BODY> Hello world!</BODY></HTML>"
    Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(responseString)
    ' Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length
    Dim output As System.IO.Stream = response.OutputStream
    output.Write(buffer, 0, buffer.Length)
    'You must close the output stream.
    output.Close()
    listener.Stop()
End Sub

설명

이 메서드를 호출하기 전에 메서드를 Start 호출하고 속성에서 반환 Prefixes 된 에 URI 문자열 HttpListenerPrefixCollection 을 추가하여 수신 대기할 URI 접두사를 하나 이상 추가해야 합니다. 접두사에 대한 자세한 설명은 클래스 개요를 HttpListener 참조하세요.

이 메서드는 들어오는 요청을 기다리는 동안 차단합니다. 사용 하 여 들어오는 요청은 애플리케이션 차단 되지 않도록 비동기적으로 (별도 스레드에서) 처리를 하려는 경우는 BeginGetContext 메서드.

호출자 참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

적용 대상