HttpListenerRequest 类

定义

描述传入 HttpListener 对象的 HTTP 请求。 此类不能被继承。

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

示例

下面的代码示例演示如何接收和响应 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

注解

当客户端向统一资源标识符发出请求 (URI) 由对象 HttpListener 处理时,该 HttpListener 对象提供一个 HttpListenerContext 对象,其中包含有关发送方、请求和发送到客户端的响应的信息。 该 HttpListenerContext.Request 属性返回 HttpListenerRequest 描述请求的对象。

HttpListenerRequest 对象包含有关请求的信息,例如请求 HttpMethod 字符串、 UserAgent 字符串和请求正文数据, (查看 InputStream 属性) 。

若要回复请求,必须使用该属性获取关联的响应 Response

属性

AcceptTypes

获取客户端接受的 MIME 类型。

ClientCertificateError

获取一个错误代码,该代码标识的问题涉及客户端提供的 X509Certificate

ContentEncoding

获取可用于随请求发送的数据的内容编码。

ContentLength64

获取包含在请求中的正文数据的长度。

ContentType

获取包含在请求中的正文数据的 MIME 类型。

Cookies

获取随请求发送的 Cookie。

HasEntityBody

获取一个 Boolean 值,该值指示请求是否有关联的正文数据。

Headers

获取在请求中发送的标头名称/值对的集合。

HttpMethod

获取由客户端指定的 HTTP 方法。

InputStream

获取包含由客户端发送的正文数据的流。

IsAuthenticated

获取一个 Boolean 值,该值指示发送此请求的客户端是否经过身份验证。

IsLocal

获取 Boolean 值,该值指示该请求是否来自本地计算机。

IsSecureConnection

获取一个 Boolean 值,该值指示用来发送请求的 TCP 连接是否使用安全套接字层 (SSL) 协议。

IsWebSocketRequest

获取 Boolean 值,该值指示 TCP 连接是否为 WebSocket 请求。

KeepAlive

获取一个 Boolean 值,该值指示客户端是否请求持久性连接。

LocalEndPoint

获取请求定向到的服务器 IP 地址和端口号。

ProtocolVersion

获取请求客户端使用的 HTTP 版本。

QueryString

获取包含在请求中的查询字符串。

RawUrl

获取客户端请求的 URL 信息(不包括主机和端口)。

RemoteEndPoint

获取发出请求的客户端 IP 地址和端口号。

RequestTraceIdentifier

获取传入 HTTP 请求的请求标识符。

ServiceName

获取客户端通过请求发送的服务提供程序名称 (SPN)。

TransportContext

获取客户端请求的 TransportContext

Url

获取客户端请求的 Uri 对象。

UrlReferrer

获取将使客户端与服务器相关的资源的统一资源标识符 (URI)。

UserAgent

获取客户端提供的用户代理。

UserHostAddress

获取请求定向到的服务器 IP 地址和端口号。

UserHostName

获取由客户端指定的 DNS 名称和端口号(如果提供了端口号)。

UserLanguages

获取响应的首选自然语言。

方法

BeginGetClientCertificate(AsyncCallback, Object)

开始对客户端的 X.509 v.3 证书的异步请求。

EndGetClientCertificate(IAsyncResult)

结束对客户端的 X.509 v.3 证书的异步请求。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetClientCertificate()

检索客户端的 X.509 v.3 证书。

GetClientCertificateAsync()

以异步操作检索客户端的 X.509 v.3 证书。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅