Dialog

SimpleHttpServer

Oct 29th, 2014
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5.  
  6. /*
  7.  * Author:
  8.  * Dialog
  9.  *
  10.  * Contact:
  11.  */
  12.  
  13. namespace SimpleHttpServer
  14. {
  15.     public class HttpServer
  16.     {
  17.         private static readonly Random Rand = new Random();
  18.         private readonly HttpListener _listener;
  19.  
  20.         public HttpServer(int port)
  21.         {
  22.             if (port < 1 || port > ushort.MaxValue)
  23.                 throw new ArgumentOutOfRangeException("Port can not be less than 1 or greater than " + ushort.MaxValue);
  24.  
  25.             _listener = new HttpListener
  26.             {
  27.                 Prefixes = { string.Format("http://*:{0}/", port) }
  28.             };
  29.  
  30.             _listener.Start();
  31.             ListenAsync();
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Listens for incoming HTTP requests on the specified port
  36.         /// </summary>
  37.         public async Task ListenAsync()
  38.         {
  39.             var context = await _listener.GetContextAsync();
  40.  
  41.             Console.WriteLine("[{0}]: Received request from {1}",
  42.                               DateTime.Now, context.Request.RemoteEndPoint);
  43.  
  44.             SendResponseAsync(context.Response);
  45.  
  46.             ListenAsync();
  47.         }
  48.  
  49.         /// <summary>
  50.         /// Generates a HTML document containing a random number and writes it to the response stream
  51.         /// </summary>
  52.         /// <param name="response">the HttpListenerResponse for the request</param>
  53.         /// <param name="encoding">specifies which encoding should be used. Default is UTF-8</param>
  54.         private static async Task SendResponseAsync(HttpListenerResponse response, Encoding encoding = null)
  55.         {
  56.             var content = string.Format("<html>" +
  57.                                         "<body>" +
  58.                                         "<h1>Random number generator</h1>" +
  59.                                         "<p>The number is: <strong>{0}</strong></p>" +
  60.                                         "</body>" +
  61.                                         "</html>", Rand.Next(1000));
  62.  
  63.             var contentEncoding = encoding ?? Encoding.UTF8;
  64.  
  65.             var buffer = contentEncoding.GetBytes(content);
  66.  
  67.             response.ContentLength64 = buffer.Length;
  68.             response.ContentType = "text/html";
  69.             response.ContentEncoding = contentEncoding;
  70.             response.StatusCode = (int) HttpStatusCode.OK;
  71.  
  72.             using (var stream = response.OutputStream)
  73.                 await stream.WriteAsync(buffer, 0, buffer.Length);
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment