Respond to HTTP Requests from Within Your Application - CSharp Network

CSharp examples for Network:URL

Description

Respond to HTTP Requests from Within Your Application

Demo Code

using System;/*w w w  .  j  a  va  2  s.  c  om*/
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
class MainClass
{
   private static int maxRequestHandlers = 5;
   private static int requestHandlerID = 0;
   private static HttpListener listener;
   private static void RequestHandler(IAsyncResult result)
   {
      Console.WriteLine("{0}: Activated.", result.AsyncState);
      try
      {
         HttpListenerContext context = listener.EndGetContext(result);
         Console.WriteLine("{0}: Processing HTTP Request from {1} ({2}).",
         result.AsyncState,
         context.Request.UserHostName,
         context.Request.RemoteEndPoint);
         StreamWriter sw = new StreamWriter(context.Response.OutputStream, Encoding.UTF8);
         sw.WriteLine("<html>");
         sw.WriteLine("<head>");
         sw.WriteLine("<title>test</title>");
         sw.WriteLine("</head>");
         sw.WriteLine("<body>");
         sw.WriteLine("Content: " + result.AsyncState);
         sw.WriteLine("</body>");
         sw.WriteLine("</html>");
         sw.Flush();
         // Configure the Response.
         context.Response.ContentType = "text/html";
         context.Response.ContentEncoding = Encoding.UTF8;
         // Close the Response to send it to the client.
         context.Response.Close();
         Console.WriteLine("{0}: Sent HTTP response.", result.AsyncState);
      }
      catch (ObjectDisposedException)
      {
         Console.WriteLine("{0}: HttpListener disposed--shutting down.",
         result.AsyncState);
      }
      finally
      {
         if (listener.IsListening)
         {
            Console.WriteLine("{0}: Creating new request handler.", result.AsyncState);
            listener.BeginGetContext(RequestHandler, "RequestHandler_" + Interlocked.Increment(ref requestHandlerID));
         }
      }
   }
   public static void Main(string[] args)
   {
      if (!HttpListener.IsSupported)
      {
         Console.WriteLine("No HttpListener.");
         return;
      }
      using (listener = new HttpListener())
      {
         listener.Prefixes.Add("http://localhost:8080/test/");
         listener.Prefixes.Add("http://localhost:80/test/");
         Console.WriteLine("Starting HTTP Server");
         listener.Start();
         Console.WriteLine("HTTP Server started");
         Console.WriteLine(Environment.NewLine);
         for (int count = 0; count < maxRequestHandlers; count++)
         {
            listener.BeginGetContext(RequestHandler, "RequestHandler_" + Interlocked.Increment(ref requestHandlerID));
         }
         // Wait for the user to stop the HttpListener.
         Console.WriteLine("Press Enter to stop the HTTP Server");
         Console.ReadLine();
         listener.Stop();
         listener.Abort();
      }
   }
}

Result


Related Tutorials