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 的資訊,請參閱 HttpListerner 的補充 API 備註

建構函式

名稱 Description
HttpListener()

初始化 HttpListener 類別的新執行個體。

屬性

名稱 Description
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)連線的額外請求來進行認證。

方法

名稱 Description
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)

明確介面實作

名稱 Description
IDisposable.Dispose()

釋放此 HttpListener 物體所持有的資源。

適用於

另請參閱