To further simplify listeners for the HTTP protocol, . .NET provides the HttpListener class in the namespace System.Net. Accompanying this object, . .NET provides a series of related objects that encapsulate HTTP processing work. Note that this class uses Http.sys system components to get the job done, so it can only be used on Windows XPSP2 or Server 2003 or later operating systems.
The HttpListener class further simplifies the listening operation, and only needs to provide the listening address, port number, and virtual path through the string method to start the listening work.
After starting listening, the GetContext method will block the thread, when the client's request arrives, HttpListener returns an HttpListenerContext object as the general proxy for processing client requests, through the Request property of the proxy object, we can get an object of type HttpListenerRequest representing the request parameters, this object objectifies most of the request parameters, so , we can get the request parameters through a series of properties it provides. For example, the HttpMethod property of HttpListenerRequest provides the method type requested. Through the Response attribute of the proxy, we can obtain a response processing object of type HttpListenerResponse, which encapsulates the data and operations of the response, which greatly simplifies the programming workload of the response, and the working process is as follows:
//检查系统是否支持 if (! HttpListener.IsSupported)
{ throw new System.InvalidOperationException( "To use HttpListener, you must be on Windows XP SP2 or Server 2003 or later!" );
} Note that the prefix must end with a / forward slash string[] prefixes = new string[] { "http://localhost:49152/" }; Create a listener. HttpListener listener = new HttpListener(); Add the prefix of the monitor. foreach (string s in prefixes)
{ listener. Prefixes.Add(s);
} Start listening listener. Start(); Console.WriteLine(" listening..."); while (true)
{ Note: The GetContext method will block the thread until the request arrives HttpListenerContext context = listener. GetContext(); Get the request object HttpListenerRequest request = context. Request; Console.WriteLine("{0} {1} HTTP/1.1", request. HttpMethod, request. RawUrl); Console.WriteLine("Accept: {0}", string. Join(",", request. AcceptTypes)); Console.WriteLine("Accept-Language: {0}", string. Join(",", request. UserLanguages)); Console.WriteLine("User-Agent: {0}", request. UserAgent); Console.WriteLine("Accept-Encoding: {0}", request. Headers["Accept-Encoding"]); Console.WriteLine("Connection: {0}", request. KeepAliv e ? "Keep-Alive" : "close"); Console.WriteLine("Host: {0}", request. UserHostName); Console.WriteLine("Pragma: {0}", request. Headers["Pragma"]); Get the response target HttpListenerResponse response = context. Response; Construct the response content string responseString = @"<html> <head><title>From HttpListener Server</title></head> <body><h1>Hello, world.</h1></body> </html>"; Set the response header content, length, encoding response. ContentLength64 = System.Text.Encoding.UTF8.GetByteCount(responseString); response. ContentType = "text/html; charset=UTF-8"; Output responses System.IO.Stream output = response. OutputStream; System.IO.StreamWriter writer = new System.IO.StreamWriter(output); writer. Write(responseString); The output stream must be turned off writer. Close(); if (Console.KeyAvailable) break;
} Shut down the server listener. Stop();
When using HttpListener, commonly used request and response parameters become object properties, which greatly reduces the programming workload. However, most of the parameters still need to be accessed through the Headers indexer, just like the Accept-Encoding request parameter in the example above, which we cannot access directly through the properties. |