CustomHttpServer.cs 851 B

123456789101112131415161718192021222324252627282930
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace HttpTest.HttpService
  8. {
  9. public class CustomHttpServer : HttpServerBase
  10. {
  11. public CustomHttpServer(int maxThreads) : base(maxThreads)
  12. {
  13. }
  14. protected override void ProcessHttpRequest(HttpListenerContext ctx)
  15. {
  16. Console.WriteLine("You can do anything......");
  17. string response = "Hello World!";
  18. byte[] buffer = Encoding.UTF8.GetBytes(response);
  19. ctx.Response.StatusCode = 200;
  20. ctx.Response.ContentLength64 = buffer.Length;
  21. ctx.Response.OutputStream.Write(buffer, 0, buffer.Length);
  22. ctx.Response.OutputStream.Close();
  23. ctx.Response.Close();
  24. }
  25. }
  26. }