HttpListenerPrefixCollection.Add メソッド
アセンブリ: System (system.dll 内)


例外の種類 | 条件 |
---|---|
ArgumentNullException | uriPrefix が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException | uriPrefix が、http:// または https:// スキームを使用していません。HttpListener オブジェクトでは、これらのスキームのみがサポートされています。 または uriPrefix が、正しい書式の URI プレフィックスではありません。文字列は必ず "/" で終わる必要があります。 |
ObjectDisposedException | |
HttpListenerException | Windows 関数呼び出しが失敗しました。例外の ErrorCode プロパティをチェックして、例外の原因を確認してください。この例外は、別の HttpListener が既にプレフィックス uriPrefix を追加している場合にスローされます。 |

このメソッドは、関連付けられた HttpListener オブジェクトによって管理されているプレフィックスのセットに、URI プレフィックスを追加します。uriPrefix をチェックして有効であることを確認しているときは、大文字と小文字の区別が無視されます。
URI プレフィックス文字列は、"http://www.contoso.com:8080/customerData/" のように、スキーム (http または https)、ホスト、ポート (省略可能)、およびパス (省略可能) で構成されます。プレフィックスは、スラッシュ ("/") で終了する必要があります。要求された URI に最も一致するプレフィックスを持つ HttpListener が要求に応答します。複数の HttpListener オブジェクトが同じプレフィックスを追加することはできません。既に使用中のプレフィックスを HttpListener が追加した場合は、HttpListenerException 例外がスローされます。
ポートが指定されている場合、ホスト要素を "*" に置き換えて、要求された URI が他のプレフィックスに一致しない場合に HttpListener がそのポートに送信された要求を受け入れることを示すことができます。たとえば、要求された URI が他のどの HttpListener でも処理されない場合に、ポート 8080 に送信されたすべての要求を受信するようにするには、プレフィックスを "http://*:8080/" にします。同様に、HttpListener が、あるポートに送信されたすべての要求を受け入れることを指定するには、ホスト要素を "+" という文字に置き換えて、"https://+:8080/" のようにします。"*" および "+" の文字は、パスを含んだプレフィックスに使用できます。

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(); }


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からHttpListenerPrefixCollection.Add メソッドを検索する場合は、下記のリンクをクリックしてください。

- HttpListenerPrefixCollection.Add メソッドのページへのリンク