HttpListener 类

定义

提供一个简单的以编程方式控制的 HTTP 协议侦听器。 此类不能被继承。

public ref class HttpListener sealed : IDisposable
public sealed class HttpListener : IDisposable
type HttpListener = class
    interface IDisposable
Public NotInheritable Class HttpListener
Implements IDisposable
继承
HttpListener
实现

示例

// 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

注解

有关此 API 的详细信息,请参阅 HttpListener 的补充 API 备注

构造函数

名称 说明
HttpListener()

初始化 HttpListener 类的新实例。

属性

名称 说明
AuthenticationSchemes

获取或设置用于对客户端进行身份验证的方案。

AuthenticationSchemeSelectorDelegate

获取或设置调用的委托,以确定用于对客户端进行身份验证的协议。

DefaultServiceNames

获取由已注册前缀确定的服务提供程序名称(SPN)的默认列表。

ExtendedProtectionPolicy

获取或设置 ExtendedProtectionPolicy 用于会话的扩展保护。

ExtendedProtectionSelectorDelegate

获取或设置调用的委托,以确定 ExtendedProtectionPolicy 要用于每个请求。

IgnoreWriteExceptions

获取或设置一个 Boolean 值,该值指定应用程序是否收到在向客户端发送响应时 HttpListener 发生的异常。

IsListening

获取一个值,该值指示是否已 HttpListener 启动。

IsSupported

获取一个值,该值指示是否可以 HttpListener 与当前操作系统一起使用。

Prefixes

获取此 HttpListener 对象处理的统一资源标识符(URI)前缀。

Realm

获取或设置与此 HttpListener 对象关联的领域或资源分区。

TimeoutManager

HttpListener 实例的超时管理器。

UnsafeConnectionNtlmAuthentication

获取或设置一个值,该值控制是否在使用 NTLM 时,需要使用同一 Boolean 传输控制协议(TCP)连接的其他请求进行身份验证。

方法

名称 说明
Abort()

立即关闭 HttpListener 对象,丢弃所有当前排队的请求。

BeginGetContext(AsyncCallback, Object)

开始异步检索传入请求。

Close()

HttpListener关闭 .

EndGetContext(IAsyncResult)

完成异步操作以检索传入的客户端请求。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetContext()

等待传入请求,并在收到一个请求时返回。

GetContextAsync()

等待传入请求作为异步操作。

GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Start()

允许此实例接收传入的请求。

Stop()

导致此实例停止接收新的传入请求并终止处理所有正在进行的请求。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

显式接口实现

名称 说明
IDisposable.Dispose()

释放此 HttpListener 对象持有的资源。

适用于

另请参阅