Program.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Net;
  2. using System.Text;
  3. using System.Text.Json;
  4. using Sisk.Cadente;
  5. HttpHost.QueueSize = 4096;
  6. var host = new HttpHost ( new IPEndPoint ( IPAddress.Any, 8080 ) );
  7. host.ContextCreated += Host_ContextCreated;
  8. host.Start ();
  9. Thread.Sleep ( Timeout.Infinite );
  10. void Host_ContextCreated ( HttpHost sender, HttpHostContext session ) {
  11. var request = session.Request;
  12. if (request.Path == "/plaintext") {
  13. SerializePlainTextResponse ( session.Response );
  14. }
  15. else if (request.Path == "/json") {
  16. SerializeJsonResponse ( session.Response );
  17. }
  18. else {
  19. session.Response.StatusCode = 404;
  20. }
  21. }
  22. static void SerializePlainTextResponse ( HttpHostContext.HttpResponse response ) {
  23. var messageBytes = Encoding.UTF8.GetBytes ( "Hello, World!" );
  24. response.Headers.Add ( new HttpHeader ( "Content-Type", "text/plain; charset=UTF-8" ) );
  25. response.ResponseStream = new MemoryStream ( messageBytes );
  26. }
  27. static void SerializeJsonResponse ( HttpHostContext.HttpResponse response ) {
  28. var contentBytes = JsonSerializer.SerializeToUtf8Bytes ( new {
  29. message = "Hello, World!"
  30. } );
  31. response.Headers.Add ( new HttpHeader ( "Content-Type", "application/json" ) );
  32. response.ResponseStream = new MemoryStream ( contentBytes );
  33. }