HttpListenerRequest 類別

定義

描述 HttpListener 物件的連入 HTTP 要求。Describes an incoming HTTP request to an HttpListener object. 這個類別無法被繼承。This class cannot be inherited.

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

範例

下列程式碼範例示範如何接收和回應 HttpListenerRequestThe following code example demonstrates how to receive and respond to a HttpListenerRequest.

// 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

備註

當用戶端對 HttpListener 物件所處理的統一資源識別元(URI)提出要求時,HttpListener 會提供 HttpListenerContext 物件,其中包含傳送給用戶端之寄件者、要求和回應的相關資訊。When a client makes a request to a Uniform Resource Identifier (URI) handled by an HttpListener object, the HttpListener provides a HttpListenerContext object that contains information about the sender, the request, and the response that is sent to the client. HttpListenerContext.Request 屬性會傳回描述要求的 HttpListenerRequest 物件。The HttpListenerContext.Request property returns the HttpListenerRequest object that describes the request.

HttpListenerRequest 物件包含要求的相關資訊,例如要求 HttpMethod 字串、UserAgent 字串和要求主體資料(請參閱 InputStream 屬性)。The HttpListenerRequest object contains information about the request, such as the request HttpMethod string, UserAgent string, and request body data (see the InputStream property).

若要回復要求,您必須使用 Response 屬性來取得相關聯的回應。To reply to the request, you must get the associated response using the Response property.

屬性

AcceptTypes

取得用戶端接受的 MIME 類型。Gets the MIME types accepted by the client.

ClientCertificateError

取得錯誤碼,識別用戶端所提供之 X509Certificate 的問題。Gets an error code that identifies a problem with the X509Certificate provided by the client.

ContentEncoding

取得可用於隨要求所傳送資料的內容編碼方式。Gets the content encoding that can be used with data sent with the request.

ContentLength64

取得要求中所包含之主體資料的長度。Gets the length of the body data included in the request.

ContentType

取得要求中所包含之主體資料的 MIME 類型。Gets the MIME type of the body data included in the request.

Cookies

取得隨要求傳送的 Cookie。Gets the cookies sent with the request.

HasEntityBody

取得 Boolean 值,指出要求是否有關聯的主體資料。Gets a Boolean value that indicates whether the request has associated body data.

Headers

取得在要求中所傳送之標頭名稱/值組的集合。Gets the collection of header name/value pairs sent in the request.

HttpMethod

取得用戶端指定的 HTTP 方法。Gets the HTTP method specified by the client.

InputStream

取得包含用戶端所傳送之主體資料的資料流。Gets a stream that contains the body data sent by the client.

IsAuthenticated

取得 Boolean 值,指出傳送這個要求的用戶端是否已經過驗證。Gets a Boolean value that indicates whether the client sending this request is authenticated.

IsLocal

取得 Boolean 值,指出要求是否從本機電腦送出。Gets a Boolean value that indicates whether the request is sent from the local computer.

IsSecureConnection

取得 Boolean 值,指出用來傳送要求的 TCP 連線是否使用 Secure Sockets Layer (SSL) 通訊協定。Gets a Boolean value that indicates whether the TCP connection used to send the request is using the Secure Sockets Layer (SSL) protocol.

IsWebSocketRequest

取得 Boolean 值,指出 TCP 連接是否為 WebSocket 要求。Gets a Boolean value that indicates whether the TCP connection was a WebSocket request.

KeepAlive

取得 Boolean 值,指出用戶端是否要求持續性連線。Gets a Boolean value that indicates whether the client requests a persistent connection.

LocalEndPoint

取得要求所導向到的伺服器 IP 位址和連接埠編號。Gets the server IP address and port number to which the request is directed.

ProtocolVersion

取得要求的用戶端所使用的 HTTP 版本。Gets the HTTP version used by the requesting client.

QueryString

取得要求中所包含的查詢字串。Gets the query string included in the request.

RawUrl

取得用戶端所要求的 URL 資訊 (不含主機和連接埠)。Gets the URL information (without the host and port) requested by the client.

RemoteEndPoint

取得起始要求的用戶端 IP 位址和連接埠編號。Gets the client IP address and port number from which the request originated.

RequestTraceIdentifier

取得連入之 HTTP 要求的要求識別項。Gets the request identifier of the incoming HTTP request.

ServiceName

取得用戶端透過要求傳送的服務提供者名稱 (SPN)。Gets the Service Provider Name (SPN) that the client sent on the request.

TransportContext

取得用戶端要求的 TransportContextGets the TransportContext for the client request.

Url

取得用戶端所要求的 Uri 物件。Gets the Uri object requested by the client.

UrlReferrer

取得用戶端用來參考伺服器之資源的統一資源識別元 (URI)。Gets the Uniform Resource Identifier (URI) of the resource that referred the client to the server.

UserAgent

取得用戶端所表示的使用者代理程式。Gets the user agent presented by the client.

UserHostAddress

取得要求所導向到的伺服器 IP 位址和連接埠編號。Gets the server IP address and port number to which the request is directed.

UserHostName

取得 DNS 名稱和用戶端所指定的連接埠編號 (如果有提供的話)。Gets the DNS name and, if provided, the port number specified by the client.

UserLanguages

取得回應慣用的自然語言。Gets the natural languages that are preferred for the response.

方法

BeginGetClientCertificate(AsyncCallback, Object)

開始用戶端之 X.509 v.3 憑證的非同步要求。Begins an asynchronous request for the client's X.509 v.3 certificate.

EndGetClientCertificate(IAsyncResult)

結束用戶端之 X.509 v.3 憑證的非同步要求。Ends an asynchronous request for the client's X.509 v.3 certificate.

Equals(Object)

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

(繼承來源 Object)
GetClientCertificate()

擷取用戶端的 X.509 v.3 憑證。Retrieves the client's X.509 v.3 certificate.

GetClientCertificateAsync()

以非同步作業的方式,擷取用戶端的 X.509 v.3 憑證。Retrieves the client's X.509 v.3 certificate as an asynchronous operation.

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)

適用於

另請參閱