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

取得或設定 Boolean 值,控制使用 NTLM 時,是否必須驗證其他使用相同傳輸控制通訊協定 (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 物件所持有的資源。

適用於

另請參閱