HttpListenerPrefixCollection.Add(String) 메서드

정의

컬렉션에 URI(Uniform Resource Identifier) 접두사를 추가합니다.

public:
 virtual void Add(System::String ^ uriPrefix);
public void Add (string uriPrefix);
abstract member Add : string -> unit
override this.Add : string -> unit
Public Sub Add (uriPrefix As String)

매개 변수

uriPrefix
String

들어오는 요청에서 비교되는 URI 정보를 식별하는 String입니다. 이 접두사는 슬래시("/")로 종결되어야 합니다.

구현

예외

uriPrefix이(가) null인 경우

uriPrefix가 http:// 또는 https:// 체계를 사용하지 않는 경우 이것이 HttpListener 개체에 대해 유일하게 지원되는 스키마입니다.

또는

uriPrefix가 올바른 형식의 URI 접두사가 아닌 경우. 이 문자열이 "/"로 종료되는지 확인합니다.

이 컬렉션과 연결된 HttpListener는 닫힙니다.

Windows 함수 호출이 실패한 경우. 예외의 ErrorCode 속성을 검토하여 예외의 원인을 확인할 수 있습니다. 다른 HttpListener에서 uriPrefix 접두사를 이미 추가한 경우 이 예외가 throw됩니다.

예제

다음 코드 예제에서는 를 HttpListener 만들고 사용자 지정 접두사를 해당 HttpListenerPrefixCollection에 추가합니다.

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

설명

이 메서드는 연결된 HttpListener 개체에서 관리하는 접두사 집합에 URI 접두사를 추가합니다. 유효한지 확인할 uriPrefix 때 대/소문자를 무시합니다.

URI 접두사 문자열은 스키마(http 또는 https), 호스트, 선택적 포트 및 선택적 경로(예: "http://www.contoso.com:8080/customerData/")로 구성됩니다. 이 접두사는 슬래시("/")로 종결되어야 합니다. HttpListener 요청된 URI와 가장 일치하는 접두사를 가진 는 요청에 응답합니다. 여러 HttpListener 개체가 동일한 접두사를 추가할 수 없습니다. HttpListenerException 이미 사용 중인 접두사를 추가하는 경우 HttpListener 예외가 throw됩니다.

포트를 지정하면 호스트 요소를 "*"로 바꿔 요청된 URI가 다른 접두사와 일치하지 않는 경우 포트로 전송된 요청을 수락함을 HttpListener 나타낼 수 있습니다. 예를 들어 요청된 URI가 다른 HttpListener에서 처리되지 않을 때 포트 8080으로 전송된 모든 요청을 수신하려면 접두사는 "http://*:8080/"입니다. 마찬가지로 에서 포트로 전송된 HttpListener 모든 요청을 수락하도록 지정하려면 호스트 요소를 "" 문자 "+https://+:8080/"로 바꿉니다. "*" 및 "+" 문자는 경로를 포함하는 접두사에 있을 수 있습니다.

.NET 4.5.3 및 Windows 10 와일드카드 하위 도메인은 개체에서 관리하는 HttpListener URI 접두사에서 지원됩니다. 와일드카드 하위 도메인을 지정하려면 URI 접두사(예 http://*.foo.com/: )에서 호스트 이름의 일부로 "*" 문자를 사용하고 이를 HttpListenerPrefixCollection.Add 메서드에 인수로 전달합니다. 이 작업은 .NET 4.5.3 및 Windows 10 작동합니다. 이전 버전에서는 가 생성됩니다.HttpListenerException

적용 대상

추가 정보