Export (0) Print
Expand All

HttpListenerRequest Class

Describes an incoming HTTP request to an HttpListener object. This class cannot be inherited.

System.Object
  System.Net.HttpListenerRequest

Namespace:  System.Net
Assembly:  System (in System.dll)

public sealed class HttpListenerRequest

The HttpListenerRequest type exposes the following members.

  NameDescription
Public propertyAcceptTypesGets the MIME types accepted by the client.
Public propertyClientCertificateErrorGets an error code that identifies a problem with the X509Certificate provided by the client.
Public propertyContentEncodingGets the content encoding that can be used with data sent with the request
Public propertyContentLength64Gets the length of the body data included in the request.
Public propertyContentTypeGets the MIME type of the body data included in the request.
Public propertyCookiesGets the cookies sent with the request.
Public propertyHasEntityBodyGets a Boolean value that indicates whether the request has associated body data.
Public propertyHeadersGets the collection of header name/value pairs sent in the request.
Public propertyHttpMethodGets the HTTP method specified by the client.
Public propertyInputStreamGets a stream that contains the body data sent by the client.
Public propertyIsAuthenticatedGets a Boolean value that indicates whether the client sending this request is authenticated.
Public propertyIsLocalGets a Boolean value that indicates whether the request is sent from the local computer.
Public propertyIsSecureConnectionGets a Boolean value that indicates whether the TCP connection used to send the request is using the Secure Sockets Layer (SSL) protocol.
Public propertyIsWebSocketRequestGets a Boolean value that indicates whether the TCP connection was a WebSocket request.
Public propertyKeepAliveGets a Boolean value that indicates whether the client requests a persistent connection.
Public propertyLocalEndPointGet the server IP address and port number to which the request is directed.
Public propertyProtocolVersionGets the HTTP version used by the requesting client.
Public propertyQueryStringGets the query string included in the request.
Public propertyRawUrlGets the URL information (without the host and port) requested by the client.
Public propertyRemoteEndPointGets the client IP address and port number from which the request originated.
Public propertyRequestTraceIdentifierGets the request identifier of the incoming HTTP request.
Public propertyServiceNameGets the Service Provider Name (SPN) that the client sent on the request.
Public propertyTransportContextGets the TransportContext for the client request.
Public propertyUrlGets the Uri object requested by the client.
Public propertyUrlReferrerGets the Uniform Resource Identifier (URI) of the resource that referred the client to the server.
Public propertyUserAgentGets the user agent presented by the client.
Public propertyUserHostAddressGets the server IP address and port number to which the request is directed.
Public propertyUserHostNameGets the DNS name and, if provided, the port number specified by the client.
Public propertyUserLanguagesGets the natural languages that are preferred for the response.
Top

  NameDescription
Public methodBeginGetClientCertificateBegins an asynchronous request for the client's X.509 v.3 certificate.
Public methodEndGetClientCertificateEnds an asynchronous request for the client's X.509 v.3 certificate.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetClientCertificateRetrieves the client's X.509 v.3 certificate.
Public methodGetClientCertificateAsyncRetrieves the client's X.509 v.3 certificate as an asynchronous operation.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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. The HttpListenerContext.Request property returns the HttpListenerRequest object that describes the request.

The HttpListenerRequest object contains information about the request, such as the request HttpMethod string, UserAgent string, and request body data (see the InputStream property).

To reply to the request, you must get the associated response using the Response property.

The 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();
}

.NET Framework

Supported in: 4.6, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft