HttpListener.Start 方法

定义

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

public:
 void Start();
public void Start ();
member this.Start : unit -> unit
Public Sub Start ()

例外

Win32 函数调用失败。 检查异常的 ErrorCode 属性以确定导致异常的原因。

此对象已关闭。

示例

下面的代码示例演示如何使用 Start 方法开始处理传入请求。


public static void NonblockingListener(string [] prefixes)
{
    HttpListener listener = new HttpListener();
    foreach (string s in prefixes)
    {
        listener.Prefixes.Add(s);
    }
    listener.Start();
    IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback),listener);
    // Applications can do some work here while waiting for the
    // request. If no work can be done until you have processed a request,
    // use a wait handle to prevent this thread from terminating
    // while the asynchronous operation completes.
    Console.WriteLine("Waiting for request to be processed asyncronously.");
    result.AsyncWaitHandle.WaitOne();
    Console.WriteLine("Request processed asyncronously.");
    listener.Close();
}
Public Shared Sub NonblockingListener(ByVal prefixes As String())
    Dim listener As HttpListener = New HttpListener()

    For Each s As String In prefixes
        listener.Prefixes.Add(s)
    Next

    listener.Start()
    Dim result As IAsyncResult = listener.BeginGetContext(New AsyncCallback(AddressOf ListenerCallback), listener)
    ' Applications can do some work here while waiting for the 
    ' request. If no work can be done until you have processed a request,
    ' use a wait handle to prevent this thread from terminating
    ' while the asynchronous operation completes.
    Console.WriteLine("Waiting for request to be processed asyncronously.")
    result.AsyncWaitHandle.WaitOne()
    Console.WriteLine("Request processed asyncronously.")
    listener.Close()
End Sub

注解

在调用 或 BeginGetContext 方法之前,GetContext必须调用此方法。

启动 HttpListener 对象后,可以使用 Stop 方法来停止它。

注意

如果此侦听器实例使用 https,则必须安装并选择服务器证书。 否则, HttpWebRequest 对此 HttpListener 的查询将失败,并意外关闭连接。 可以使用 HttpCfg.exe配置服务器证书和其他侦听器选项。

调用方说明

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅.NET Framework中的网络跟踪

适用于