HttpListenerPrefixCollection.Add(String) Metoda

Definice

Přidá předponu identifikátoru URI (Uniform Resource Identifier) do kolekce.

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)

Parametry

uriPrefix
String

A String , který identifikuje informace identifikátoru URI porovnávané v příchozích požadavcích. Předpona musí být ukončena lomítkem ("/").

Implementuje

Výjimky

uriPrefix je null.

uriPrefix nepoužívá schéma http:// ani https://. Toto jsou jediná schémata podporovaná pro HttpListener objekty.

-nebo-

uriPrefix není správně formátovaná předpona identifikátoru URI. Ujistěte se, že je řetězec ukončen řetězcem "/".

Přidružený HttpListener k této kolekci je uzavřen.

Volání funkce Windows se nezdařilo. Zkontrolujte vlastnost výjimky ErrorCode a určete příčinu výjimky. Tato výjimka se vyvolá, pokud předponu uriPrefixuž přidal jiný.HttpListener

Příklady

Následující příklad kódu vytvoří HttpListener a přidá uživatelem zadané předpony do .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

Poznámky

Tato metoda přidá předponu identifikátoru URI do sady předpon spravovaných přidruženým HttpListener objektem. Při kontrole uriPrefix platnosti se velká a malá písmena ignorují.

Řetězec předpony identifikátoru URI se skládá ze schématu (http nebo https), hostitele, volitelného portu a volitelné cesty, například .http://www.contoso.com:8080/customerData/ Předpona musí být ukončena lomítkem ("/"). S HttpListener předponou, která nejvíce odpovídá požadovanému identifikátoru URI, odpovídá na požadavek. Více HttpListener objektů nemůže přidat stejnou předponu. Pokud HttpListenerException se přidá předpona, která se už používá, vyvolá HttpListener se výjimka.

Pokud je zadaný port, může být prvek hostitele nahrazen za ,* což znamená, že HttpListener přijímá požadavky odeslané na port, pokud požadovaný identifikátor URI neodpovídá žádné jiné předponě. Například pro příjem všech požadavků odeslaných na port 8080, když požadovaný identifikátor URI nezpracuje žádný jiný HttpListener, má předpona "http://*:8080/". Podobně pokud chcete určit, že objekt HttpListener přijímá všechny požadavky odeslané na port, nahraďte prvek host znakem "+", "https://+:8080/". Znaky "*" a "+" mohou být přítomny v předponách, které obsahují cesty.

Od verze .NET 4.5.3 a Windows 10 jsou subdomény se zástupnými čísly podporovány v předponách identifikátorů URI spravovaných objektemHttpListener. Chcete-li zadat subdoménu se zástupnými znaky, použijte znak "*" jako součást názvu hostitele v předponě identifikátoru URI: například http://*.foo.com/a předejte ho jako argument metodě HttpListenerPrefixCollection.Add. To bude fungovat v .NET 4.5.3 a Windows 10. V dřívějších verzích by to vygenerovaloHttpListenerException

Platí pro

Viz také