HttpListenerContext 類別

定義

提供對 HttpListener 類別所使用之要求和回應物件的存取。Provides access to the request and response objects used by the HttpListener class. 這個類別無法被繼承。This class cannot be inherited.

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

範例

下列程式碼範例會顯示用戶端要求的使用者資訊。The following code example displays the user information for a client request.

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

備註

這個類別會提供與HttpListener物件所處理之用戶端超文字傳輸通訊協定 (HTTP) 要求相關的資訊。This class provides the information related to a client's Hypertext Transfer Protocol (HTTP) request being processed by an HttpListener object. 這個類別也具有可讓HttpListener物件接受 WebSocket 連接的方法。This class also has methods that allow an HttpListener object to accept a WebSocket connection.

方法會傳回HttpListenerContext類別的實例。 GetContextThe GetContext method returns instances of the HttpListenerContext class. 若要取得代表用戶端要求的物件, 請使用Request屬性。To get the object that represents the client request, use the Request property. 若要取得代表將由HttpListener傳送至用戶端之回應的物件, 請Response使用屬性。To get the object that represents the response that will be sent to the client by the HttpListener, use the Response property. 若要取得傳送要求之用戶端的相關使用者資訊, 例如其登入名稱以及是否已驗證, 您可以查詢IPrincipal User屬性所傳回之物件中的屬性。To get user information about the client sending the request, such as its login name and whether it has been authenticated, you can query the properties in the IPrincipal object returned by the User property.

關閉物件會將回應傳送至用戶端, 並釋放所使用的HttpListenerContext任何資源。 HttpListenerContextClosing an HttpListenerContext object sends the response to the client and frees any resources used by the HttpListenerContext. 中止物件會捨棄用戶端的回應 (如果尚未傳送), 並釋放所使用的HttpListenerContext任何資源。 HttpListenerContextAborting an HttpListenerContext object discards the response to the client if it has not already been sent, and frees any resources used by the HttpListenerContext. 在關閉或中止HttpListenerContext物件之後, 您就無法參考其方法或屬性。After closing or aborting an HttpListenerContext object, you cannot reference its methods or properties. 如果您這樣做, 就會收到ObjectDisposedException例外狀況。If you do so, you will receive an ObjectDisposedException exception.

屬性

Request

取得 HttpListenerRequest,表示用戶端的資源要求。Gets the HttpListenerRequest that represents a client's request for a resource.

Response

取得將傳送至用戶端的 HttpListenerResponse 物件,以回應用戶端的要求。Gets the HttpListenerResponse object that will be sent to the client in response to the client's request.

User

取得用來取得用戶端識別、驗證資訊和安全性角色之物件,由這個 HttpListenerContext 物件表示用戶端的要求。Gets an object used to obtain identity, authentication information, and security roles for the client whose request is represented by this HttpListenerContext object.

方法

AcceptWebSocketAsync(String)

以非同步作業的方式接受 WebSocket 連接。Accept a WebSocket connection as an asynchronous operation.

AcceptWebSocketAsync(String, Int32, TimeSpan)

以非同步作業的方式接受 WebSocket 連接,並指定支援的 WebSocket 附屬通訊協定、接收緩衝區大小和 WebSocket Keep-alive 間隔。Accept a WebSocket connection specifying the supported WebSocket sub-protocol, receive buffer size, and WebSocket keep-alive interval as an asynchronous operation.

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

以非同步作業的方式接受 WebSocket 連接,並指定支援的 WebSocket 附屬通訊協定、接收緩衝區大小、WebSocket Keep-alive 間隔和內部緩衝區。Accept a WebSocket connection specifying the supported WebSocket sub-protocol, receive buffer size, WebSocket keep-alive interval, and the internal buffer as an asynchronous operation.

AcceptWebSocketAsync(String, TimeSpan)

以非同步作業的方式接受 WebSocket 連接,並指定支援的 WebSocket 附屬通訊協定和 WebSocket Keep-alive 間隔。Accept a WebSocket connection specifying the supported WebSocket sub-protocol and WebSocket keep-alive interval as an asynchronous operation.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱