HttpListenerRequest Class
TOC
Collapse the table of content
Expand the table of content

HttpListenerRequest Class

 

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

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

System.Object
  System.Net.HttpListenerRequest

public sealed class HttpListenerRequest

NameDescription
System_CAPS_pubpropertyAcceptTypes

Gets the MIME types accepted by the client.

System_CAPS_pubpropertyClientCertificateError

Gets an error code that identifies a problem with the X509Certificate provided by the client.

System_CAPS_pubpropertyContentEncoding

Gets the content encoding that can be used with data sent with the request

System_CAPS_pubpropertyContentLength64

Gets the length of the body data included in the request.

System_CAPS_pubpropertyContentType

Gets the MIME type of the body data included in the request.

System_CAPS_pubpropertyCookies

Gets the cookies sent with the request.

System_CAPS_pubpropertyHasEntityBody

Gets a Boolean value that indicates whether the request has associated body data.

System_CAPS_pubpropertyHeaders

Gets the collection of header name/value pairs sent in the request.

System_CAPS_pubpropertyHttpMethod

Gets the HTTP method specified by the client.

System_CAPS_pubpropertyInputStream

Gets a stream that contains the body data sent by the client.

System_CAPS_pubpropertyIsAuthenticated

Gets a Boolean value that indicates whether the client sending this request is authenticated.

System_CAPS_pubpropertyIsLocal

Gets a Boolean value that indicates whether the request is sent from the local computer.

System_CAPS_pubpropertyIsSecureConnection

Gets a Boolean value that indicates whether the TCP connection used to send the request is using the Secure Sockets Layer (SSL) protocol.

System_CAPS_pubpropertyIsWebSocketRequest

Gets a Boolean value that indicates whether the TCP connection was a WebSocket request.

System_CAPS_pubpropertyKeepAlive

Gets a Boolean value that indicates whether the client requests a persistent connection.

System_CAPS_pubpropertyLocalEndPoint

Get the server IP address and port number to which the request is directed.

System_CAPS_pubpropertyProtocolVersion

Gets the HTTP version used by the requesting client.

System_CAPS_pubpropertyQueryString

Gets the query string included in the request.

System_CAPS_pubpropertyRawUrl

Gets the URL information (without the host and port) requested by the client.

System_CAPS_pubpropertyRemoteEndPoint

Gets the client IP address and port number from which the request originated.

System_CAPS_pubpropertyRequestTraceIdentifier

Gets the request identifier of the incoming HTTP request.

System_CAPS_pubpropertyServiceName

Gets the Service Provider Name (SPN) that the client sent on the request.

System_CAPS_pubpropertyTransportContext

Gets the TransportContext for the client request.

System_CAPS_pubpropertyUrl

Gets the Uri object requested by the client.

System_CAPS_pubpropertyUrlReferrer

Gets the Uniform Resource Identifier (URI) of the resource that referred the client to the server.

System_CAPS_pubpropertyUserAgent

Gets the user agent presented by the client.

System_CAPS_pubpropertyUserHostAddress

Gets the server IP address and port number to which the request is directed.

System_CAPS_pubpropertyUserHostName

Gets the DNS name and, if provided, the port number specified by the client.

System_CAPS_pubpropertyUserLanguages

Gets the natural languages that are preferred for the response.

NameDescription
System_CAPS_pubmethodBeginGetClientCertificate(AsyncCallback, Object)

Begins an asynchronous request for the client's X.509 v.3 certificate.

System_CAPS_pubmethodEndGetClientCertificate(IAsyncResult)

Ends an asynchronous request for the client's X.509 v.3 certificate.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetClientCertificate()

Retrieves the client's X.509 v.3 certificate.

System_CAPS_pubmethodGetClientCertificateAsync()

Retrieves the client's X.509 v.3 certificate as an asynchronous operation.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

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
Available since 2.0

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

Return to top
Show:
© 2016 Microsoft