HttpListenerPrefixCollection.Add(String) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
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 ingresso. Il prefisso deve essere terminato con una barra ("/").
Implementazioni
Eccezioni
uriPrefix è null.
uriPrefix non utilizza lo schema di http:// o di https://. Questi sono gli unici schemi supportati per HttpListener gli oggetti .
oppure
uriPrefix non è un prefisso URI formattato correttamente. Assicurarsi che la stringa venga terminata con un "/".
L'oggetto HttpListener associato a questa raccolta viene chiuso.
Chiamata di funzione Windows non riuscita. Controllare la proprietà dell'eccezione ErrorCode per determinare la causa dell'eccezione. Questa eccezione viene generata se un altro HttpListener oggetto ha già aggiunto il prefisso uriPrefix.
Esempio
Nell'esempio di codice seguente viene creato un oggetto HttpListener e vengono aggiunti prefissi specificati dall'utente al relativo HttpListenerPrefixCollectionoggetto .
// 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 essere terminato con una barra ("/"). Con HttpListener il prefisso che corrisponde maggiormente a un URI richiesto risponde alla richiesta. Più HttpListener oggetti non possono aggiungere lo stesso prefisso. Viene HttpListenerException generata un'eccezione se un HttpListener oggetto aggiunge un prefisso già in uso.
Quando si specifica 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 HttpListener accetta tutte le richieste inviate a una porta, sostituire l'elemento host con il carattere "" "+https://+:8080/". I caratteri "*" e "+" possono essere presenti nei prefissi che includono percorsi.
A partire da .NET 4.5.3 e Windows 10, i sottodomini con caratteri jolly sono supportati nei prefissi URI gestiti da un oggetto HttpListener. Per specificare un sottodominio con caratteri jolly, usare il carattere "*" come parte del nome host in un prefisso URI, http://*.foo.com/ad esempio , e passarlo come argomento al metodo HttpListenerPrefixCollection.Add. Questo funzionerà su .NET 4.5.3 e Windows 10. Nelle versioni precedenti verrà generato un HttpListenerException