HttpListenerPrefixCollection.Add(String) Metodo

Definizione

Aggiunge un prefisso URI (Uniform Resource Identifier) alla raccolta.

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)

Parametri

uriPrefix
String

Oggetto String che identifica le informazioni sull'URI confrontate nelle richieste in arrivo. Il prefisso deve terminare con una barra ("/").

Implementazioni

Eccezioni

uriPrefix è null.

Il parametro uriPrefix non usa lo schema http:// o https://. Questi sono gli unici schemi supportati per gli oggetti HttpListener.

-oppure-

Il parametro uriPrefix non è un prefisso URI formattato correttamente. Assicurarsi che la stringa termini con una barra "/".

L'oggetto HttpListener associato a questa raccolta è chiuso.

Si è verificato un errore durante una chiamata a una funzione Windows. Verificare la proprietà ErrorCode dell'eccezione per determinare la causa dell'eccezione. Questa eccezione viene generata se un'altra classe HttpListener ha già aggiunto il prefisso uriPrefix.

Esempio

L'esempio di codice seguente crea un HttpListener oggetto e aggiunge i prefissi specificati dall'utente al relativo 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

Commenti

Questo metodo aggiunge un prefisso URI al set di prefissi gestiti dall'oggetto associato HttpListener . Quando si verifica uriPrefix che sia valido, il caso viene ignorato.

Una stringa di prefisso URI è costituita da uno schema (http o https), un host, una porta facoltativa e un percorso facoltativo, ad esempio "http://www.contoso.com:8080/customerData/". Il prefisso deve terminare con una barra ("/"). Con HttpListener il prefisso che corrisponde più strettamente a un URI richiesto risponde alla richiesta. Più HttpListener oggetti non possono aggiungere lo stesso prefisso. Viene HttpListenerException generata un'eccezione se un HttpListener prefisso aggiunge un prefisso già in uso.

Quando viene specificata una porta, l'elemento host può essere sostituito con "*" per indicare che le HttpListener richieste accettate inviate alla porta se l'URI richiesto non corrisponde ad alcun altro prefisso. Ad esempio, per ricevere tutte le richieste inviate alla porta 8080 quando l'URI richiesto non viene gestito da nessun altro HttpListener, il prefisso è "http://*:8080/". Analogamente, per specificare che accetta HttpListener tutte le richieste inviate a una porta, sostituire l'elemento host con il caratterehttps://+:8080/ "+". I caratteri "" e "*+" possono essere presenti nei prefissi che includono percorsi.

A partire da .NET 4.5.3 e Windows 10, i sottodomini jolly sono supportati nei prefissi URI gestiti da un HttpListener oggetto. Per specificare un sottodominio con caratteri jolly, usare il carattere "*" come parte del nome host in un prefisso URI: ad esempio , http://*.foo.com/e passare questo come argomento al metodo HttpListenerPrefixCollection.Add. Ciò funzionerà su .NET 4.5.3 e Windows 10; nelle versioni precedenti, verrà generato unHttpListenerException

Si applica a

Vedi anche