Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using System.Threading;
  5.  
  6. namespace ExampleSimpleWebserver
  7. {
  8.  
  9. class Program
  10. {
  11. public static void NonblockingListener(string[] prefixes)
  12. {
  13. HttpListener listener = new HttpListener();
  14. foreach (string s in prefixes)
  15. {
  16. listener.Prefixes.Add(s);
  17. }
  18. listener.Start();
  19. IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
  20. // Applications can do some work here while waiting for the
  21. // request. If no work can be done until you have processed a request,
  22. // use a wait handle to prevent this thread from terminating
  23. // while the asynchronous operation completes.
  24. Console.WriteLine("Waiting for request to be processed asyncronously.");
  25. result.AsyncWaitHandle.WaitOne();
  26. Console.WriteLine("Request processed asyncronously.");
  27. listener.Close();
  28. }
  29.  
  30. static void Main(string[] args)
  31. {
  32. Console.WriteLine("Running async server.");
  33. #region Method 1
  34. NonblockingListener(new string[] { "http://localhost:8081/", "http://127.0.0.1:8081/" });
  35. return;
  36. #endregion
  37. #region Method 2
  38. new AsyncServer();
  39. #endregion
  40. }
  41. public static void ListenerCallback(IAsyncResult result)
  42. {
  43. HttpListener listener = (HttpListener)result.AsyncState;
  44. // Call EndGetContext to complete the asynchronous operation.
  45. HttpListenerContext context = listener.EndGetContext(result);
  46. HttpListenerRequest request = context.Request;
  47. // Obtain a response object.
  48. HttpListenerResponse response = context.Response;
  49. // Construct a response.
  50. string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
  51. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
  52. // Get a response stream and write the response to it.
  53. response.ContentLength64 = buffer.Length;
  54. System.IO.Stream output = response.OutputStream;
  55. output.Write(buffer, 0, buffer.Length);
  56. // You must close the output stream.
  57. output.Close();
  58. }
  59. }
  60.  
  61. public class AsyncServer
  62. {
  63. public AsyncServer()
  64. {
  65. var listener = new HttpListener();
  66.  
  67. listener.Prefixes.Add("http://localhost:8081/");
  68. listener.Prefixes.Add("http://127.0.0.1:8081/");
  69.  
  70. listener.Start();
  71.  
  72. while (true)
  73. {
  74. try
  75. {
  76. var context = listener.GetContext();
  77. ThreadPool.QueueUserWorkItem(o => HandleRequest(context));
  78. }
  79. catch (Exception)
  80. {
  81. // Ignored for this example
  82. }
  83. }
  84. }
  85.  
  86. private void HandleRequest(object state)
  87. {
  88. try
  89. {
  90. var context = (HttpListenerContext)state;
  91.  
  92. context.Response.StatusCode = 200;
  93. context.Response.SendChunked = true;
  94.  
  95. var sb = new StringBuilder("<ul>");
  96. foreach (string key in context.Request.QueryString.Keys)
  97. {
  98. sb.AppendFormat("<li>{0}={1}</li>", key, context.Request.QueryString[key]);
  99. }
  100. sb.Append("</ul>");
  101. var bytes = Encoding.UTF8.GetBytes(sb.ToString());
  102. context.Response.OutputStream.Write(bytes, 0, bytes.Length);
  103. context.Response.OutputStream.Flush();
  104. }
  105. catch (Exception)
  106. {
  107. // Client disconnected or some other error - ignored for this example
  108. }
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement