Ejemplo n.º 1
0
 /// <summary>
 /// Context recieved callback
 /// </summary>
 /// <param name="asyncResult">Asynchronous result</param>
 private void ContextReceivedCallback(IAsyncResult asyncResult)
 {
     try
     {
         if ((webPages != null) && (HTTPListener != null))
         {
             if (HTTPListener.IsListening)
             {
                 HttpListenerContext context = HTTPListener.EndGetContext(asyncResult);
                 HTTPListener.BeginGetContext(new AsyncCallback(ContextReceivedCallback), null);
                 context.Response.ContentEncoding = Encoding.UTF8;
                 using (Stream stream = context.Response.OutputStream)
                 {
                     using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
                     {
                         IWebPage web_page = webPages.GetWebPage(context.Request.RawUrl);
                         if (web_page == null)
                         {
                             context.Response.StatusCode = 404;
                             writer.Write("Not available!");
                         }
                         else
                         {
                             context.Response.StatusCode = 200;
                             writer.Write(web_page.GetContent(context));
                         }
                     }
                 }
             }
         }
     }
     catch (Exception e)
     {
         Console.Error.WriteLine(e);
     }
 }