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

获取一个错误代码,该代码标识的问题涉及客户端提供的 X509CertificateGets 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 连接是否使用安全套接字层 (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)

适用于

另请参阅