HttpListenerContext 類別

定義

提供對 HttpListener 類別所使用之要求和回應物件的存取。 此類別無法獲得繼承。

public ref class HttpListenerContext sealed
public sealed class HttpListenerContext
type HttpListenerContext = class
Public NotInheritable Class HttpListenerContext
繼承
HttpListenerContext

範例

下列程式代碼範例會顯示用戶端要求的用戶資訊。

public static string ClientInformation(HttpListenerContext context)
{
    System.Security.Principal.IPrincipal user = context.User;
    System.Security.Principal.IIdentity id = user.Identity;
    if (id == null)
    {
        return "Client authentication is not enabled for this Web server.";
    }

    string display;
    if (id.IsAuthenticated)
    {
        display = String.Format("{0} was authenticated using {1}", id.Name,
            id.AuthenticationType);
    }
    else
    {
       display = String.Format("{0} was not authenticated", id.Name);
    }
    return display;
}
Public Shared Function ClientInformation(ByVal context As HttpListenerContext) As String
    Dim user As System.Security.Principal.IPrincipal = context.User
    Dim id As System.Security.Principal.IIdentity = user.Identity

    If id Is Nothing Then
        Return "Client authentication is not enabled for this Web server."
    End If

    Dim display As String

    If id.IsAuthenticated Then
        display = String.Format("{0} was authenticated using {1}", id.Name, id.AuthenticationType)
    Else
        display = String.Format("{0} was not authenticated", id.Name)
    End If

    Return display
End Function

備註

這個類別提供用戶端的超文本傳輸通訊協定的相關信息, (HTTP) 物件正在 HttpListener 處理的要求。 這個類別也有方法可讓 HttpListener 物件接受 WebSocket 連線。

方法會 GetContext 傳回 類別的 HttpListenerContext 實例。 若要取得代表用戶端要求的 物件,請使用 Request 屬性。 若要取得 物件,此物件表示將由 傳送至用戶端 HttpListener的回應,請使用 Response 屬性。 若要取得傳送要求之客戶端的使用者資訊,例如其登入名稱,以及是否已通過驗證,您可以查詢 屬性所User傳回之 物件中的IPrincipal屬性。

HttpListenerContext關閉物件會將回應傳送給用戶端,並釋放 所使用的HttpListenerContext任何資源。 如果尚未傳送物件,中止 HttpListenerContext 物件會捨棄對客戶端的 HttpListenerContext回應,並釋放 所使用的任何資源。 關閉或中止 HttpListenerContext 物件之後,您無法參考其方法或屬性。 如果您這樣做,將會收到例外狀況 ObjectDisposedException

屬性

Request

取得 HttpListenerRequest,表示用戶端的資源要求。

Response

取得將傳送至用戶端的 HttpListenerResponse 物件,以回應用戶端的要求。

User

取得用來取得用戶端識別、驗證資訊和安全性角色之物件,由這個 HttpListenerContext 物件表示用戶端的要求。

方法

AcceptWebSocketAsync(String)

以非同步作業的方式接受 WebSocket 連接。

AcceptWebSocketAsync(String, Int32, TimeSpan)

以非同步作業的方式接受 WebSocket 連接,並指定支援的 WebSocket 附屬通訊協定、接收緩衝區大小和 WebSocket Keep-alive 間隔。

AcceptWebSocketAsync(String, Int32, TimeSpan, ArraySegment<Byte>)

以非同步作業的方式接受 WebSocket 連接,並指定支援的 WebSocket 附屬通訊協定、接收緩衝區大小、WebSocket Keep-alive 間隔和內部緩衝區。

AcceptWebSocketAsync(String, TimeSpan)

接受 WebSocket 連線,指定支援的 WebSocket 子通訊協定和 WebSocket 保持運作間隔做為異步操作。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱