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。 プレフィックスは、スラッシュ ("/") で終了する必要があります。

実装

例外

uriPrefixnullです。

uriPrefix が、http:// または https:// スキームを使用していません。 HttpListener オブジェクトでは、これらのスキームのみがサポートされています。

- または -

uriPrefix が、正しい書式の URI プレフィックスではありません。 文字列が "/" で終わっていることを確認してください。

このコレクションに関連付けられている HttpListener は閉じています。

Windows の関数呼び出しが失敗しました。 例外の ErrorCode プロパティを調べて、例外の原因を確認します。 この例外は、別の HttpListener が既にプレフィックス uriPrefix を追加している場合にスローされます。

次のコード例では、 を 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使用されているプレフィックスを追加すると、例外がスローされます。

ポートを指定すると、ホスト要素を "*" に置き換えて、要求された URI が HttpListener 他のプレフィックスと一致しない場合にポートに送信された要求を受け入れることを示すことができます。 たとえば、要求された URI が他 HttpListenerの URI によって処理されないときにポート 8080 に送信されたすべての要求を受信するには、プレフィックスは "http://*:8080/" です。 同様に、 がHttpListenerポートに送信されたすべての要求を受け入れるように指定するには、host 要素を "" 文字 "+https://+:8080/" に置き換えます。 "*" 文字と "+" 文字は、パスを含むプレフィックスに含めることができます。

.NET 4.5.3 および Windows 10 以降では、オブジェクトによってHttpListener管理される URI プレフィックスでワイルドカード サブドメインがサポートされています。 ワイルドカード サブドメインを指定するには、URI プレフィックス内のホスト名の一部として "*" 文字を使用し、 http://*.foo.com/これを引数として HttpListenerPrefixCollection.Add メソッドに渡します。 これは .NET 4.5.3 および Windows 10 で動作します。以前のバージョンでは、HttpListenerException

適用対象

こちらもご覧ください