HttpListenerPrefixCollection.Add(String) 方法

定義

將統一資源識別元 (URI) 前置詞加入至集合。

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

String,可識別要在連入要求中進行比較的 URI 資訊。 前置詞必須以斜線 ("/") 結束。

實作

例外狀況

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

備註

這個方法會將 URI 前置詞新增至相關聯 HttpListener 物件所管理的前置詞集。 檢查 uriPrefix 以確保其有效時,會忽略大小寫。

URI 前置詞字串是由 HTTP 或 HTTPs () 、主機、選擇性埠和選擇性路徑所組成,例如 “http://www.contoso.com:8080/customerData/”。 前置詞必須以斜線 ("/") 結束。 HttpListener,其前置詞最符合要求的 URI 會回應要求。 多個 HttpListener 物件無法新增相同的前置詞。 HttpListenerException如果 HttpListener 已加入已在使用中的前置詞,則會擲回例外狀況。

指定埠時,可以將主機元素取代為 “*”,表示 HttpListener 如果要求的 URI 不符合任何其他前置詞,則接受傳送至埠的要求。 例如,當要求 URI 未由任何其他 HttpListener處理時,若要接收所有傳送至埠 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

適用於

另請參閱