Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Text;
- using System.Threading;
- namespace ExampleSimpleWebserver
- {
- class Program
- {
- public static void NonblockingListener(string[] prefixes)
- {
- HttpListener listener = new HttpListener();
- foreach (string s in prefixes)
- {
- listener.Prefixes.Add(s);
- }
- listener.Start();
- IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
- // Applications can do some work here while waiting for the
- // request. If no work can be done until you have processed a request,
- // use a wait handle to prevent this thread from terminating
- // while the asynchronous operation completes.
- Console.WriteLine("Waiting for request to be processed asyncronously.");
- result.AsyncWaitHandle.WaitOne();
- Console.WriteLine("Request processed asyncronously.");
- listener.Close();
- }
- static void Main(string[] args)
- {
- Console.WriteLine("Running async server.");
- #region Method 1
- NonblockingListener(new string[] { "http://localhost:8081/", "http://127.0.0.1:8081/" });
- return;
- #endregion
- #region Method 2
- new AsyncServer();
- #endregion
- }
- public static void ListenerCallback(IAsyncResult result)
- {
- HttpListener listener = (HttpListener)result.AsyncState;
- // Call EndGetContext to complete the asynchronous operation.
- HttpListenerContext context = listener.EndGetContext(result);
- 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();
- }
- }
- public class AsyncServer
- {
- public AsyncServer()
- {
- var listener = new HttpListener();
- listener.Prefixes.Add("http://localhost:8081/");
- listener.Prefixes.Add("http://127.0.0.1:8081/");
- listener.Start();
- while (true)
- {
- try
- {
- var context = listener.GetContext();
- ThreadPool.QueueUserWorkItem(o => HandleRequest(context));
- }
- catch (Exception)
- {
- // Ignored for this example
- }
- }
- }
- private void HandleRequest(object state)
- {
- try
- {
- var context = (HttpListenerContext)state;
- context.Response.StatusCode = 200;
- context.Response.SendChunked = true;
- var sb = new StringBuilder("<ul>");
- foreach (string key in context.Request.QueryString.Keys)
- {
- sb.AppendFormat("<li>{0}={1}</li>", key, context.Request.QueryString[key]);
- }
- sb.Append("</ul>");
- var bytes = Encoding.UTF8.GetBytes(sb.ToString());
- context.Response.OutputStream.Write(bytes, 0, bytes.Length);
- context.Response.OutputStream.Flush();
- }
- catch (Exception)
- {
- // Client disconnected or some other error - ignored for this example
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement