HttpListenerPrefixCollection.Add(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将统一资源标识符 (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
为 null
。
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 接受发送到端口的所有请求,请将主机元素替换为“+
”字符“”https://+:8080/
。 “”*
和“”+
字符可以出现在包含路径的前缀中。
从 .NET 4.5.3 和 Windows 10 开始,通配符子域在对象HttpListener管理的 URI 前缀中受支持。 若要指定通配符子域,请在 URI 前缀中使用“*”字符作为主机名的一部分:例如, http://*.foo.com/
将此作为参数传递给 HttpListenerPrefixCollection.Add 方法。 这将在 .NET 4.5.3 和 Windows 10;在早期版本中,这将生成一个HttpListenerException