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
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();
}
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
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.
Properties AcceptTypesGets the MIME types accepted by the client.
ClientCertificateErrorGets an error code that identifies a problem with the X509Certificate provided by the client.
ContentEncodingGets the content encoding that can be used with data sent with the request.
ContentLength64Gets the length of the body data included in the request.
ContentTypeGets the MIME type of the body data included in the request.
CookiesGets the cookies sent with the request.
HasEntityBodyGets a Boolean value that indicates whether the request has associated body data.
HeadersGets the collection of header name/value pairs sent in the request.
HttpMethodGets the HTTP method specified by the client.
InputStreamGets a stream that contains the body data sent by the client.
IsAuthenticatedGets a Boolean value that indicates whether the client sending this request is authenticated.
IsLocalGets a Boolean value that indicates whether the request is sent from the local computer.
IsSecureConnectionGets a Boolean value that indicates whether the TCP connection used to send the request is using the Secure Sockets Layer (SSL) protocol.
IsWebSocketRequestGets a Boolean value that indicates whether the TCP connection was a WebSocket request.
KeepAliveGets a Boolean value that indicates whether the client requests a persistent connection.
LocalEndPointGets the server IP address and port number to which the request is directed.
ProtocolVersionGets the HTTP version used by the requesting client.
QueryStringGets the query string included in the request.
RawUrlGets the URL information (without the host and port) requested by the client.
RemoteEndPointGets the client IP address and port number from which the request originated.
RequestTraceIdentifierGets the request identifier of the incoming HTTP request.
ServiceNameGets the Service Provider Name (SPN) that the client sent on the request.
TransportContextGets the TransportContext for the client request.
UrlGets the Uri object requested by the client.
UrlReferrerGets the Uniform Resource Identifier (URI) of the resource that referred the client to the server.
UserAgentGets the user agent presented by the client.
UserHostAddressGets the server IP address and port number to which the request is directed.
UserHostNameGets the DNS name and, if provided, the port number specified by the client.
UserLanguagesGets the natural languages that are preferred for the response.
Methods See alsoCollaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide. In this articleWas this page helpful?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4