You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

103 lines
4.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace JT808.Gateway.Extensions
  8. {
  9. public static class JT808HttpContextExtensions
  10. {
  11. private const string jsonType = "application/json";
  12. public static void Http204(this HttpListenerContext context)
  13. {
  14. context.Response.StatusCode = (int)HttpStatusCode.NoContent;
  15. context.Response.KeepAlive = false;
  16. context.Response.OutputStream.Close();
  17. context.Response.Close();
  18. }
  19. public static void Http401(this HttpListenerContext context)
  20. {
  21. byte[] b = Encoding.UTF8.GetBytes("auth error");
  22. context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
  23. context.Response.ContentType = jsonType;
  24. context.Response.KeepAlive = false;
  25. context.Response.ContentLength64 = b.Length;
  26. var output = context.Response.OutputStream;
  27. output.Write(b, 0, b.Length);
  28. context.Response.OutputStream.Close();
  29. context.Response.Close();
  30. }
  31. public static void Http400(this HttpListenerContext context)
  32. {
  33. byte[] b = Encoding.UTF8.GetBytes($"sim and channel parameter required.");
  34. context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
  35. context.Response.KeepAlive = false;
  36. context.Response.ContentType = jsonType;
  37. context.Response.ContentLength64 = b.Length;
  38. var output = context.Response.OutputStream;
  39. output.Write(b, 0, b.Length);
  40. context.Response.OutputStream.Close();
  41. context.Response.Close();
  42. }
  43. public static void Http404(this HttpListenerContext context)
  44. {
  45. context.Response.StatusCode = (int)HttpStatusCode.NotFound;
  46. context.Response.KeepAlive = false;
  47. context.Response.ContentType = jsonType;
  48. context.Response.OutputStream.Close();
  49. context.Response.Close();
  50. }
  51. public static void Http405(this HttpListenerContext context)
  52. {
  53. context.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
  54. context.Response.KeepAlive = false;
  55. context.Response.ContentType = jsonType;
  56. context.Response.OutputStream.Close();
  57. context.Response.Close();
  58. }
  59. public static void Http500(this HttpListenerContext context)
  60. {
  61. byte[] b = Encoding.UTF8.GetBytes("inner error");
  62. context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
  63. context.Response.KeepAlive = false;
  64. context.Response.ContentType = jsonType;
  65. context.Response.ContentLength64 = b.Length;
  66. var output = context.Response.OutputStream;
  67. output.Write(b,0, b.Length);
  68. context.Response.OutputStream.Close();
  69. context.Response.Close();
  70. }
  71. public static void HttpSend(this HttpListenerContext context, ReadOnlyMemory<byte> buffer)
  72. {
  73. context.Response.StatusCode = (int)HttpStatusCode.OK;
  74. context.Response.KeepAlive = false;
  75. context.Response.ContentType = jsonType;
  76. context.Response.ContentLength64 = buffer.Length;
  77. context.Response.OutputStream.Write(buffer.ToArray(),0, buffer.Length);
  78. context.Response.OutputStream.Close();
  79. context.Response.Close();
  80. }
  81. public static void HttpSend(this HttpListenerContext context, string json)
  82. {
  83. byte[] b = Encoding.UTF8.GetBytes(json);
  84. context.Response.StatusCode = (int)HttpStatusCode.OK;
  85. context.Response.KeepAlive = false;
  86. context.Response.ContentType = jsonType;
  87. context.Response.ContentLength64 = b.Length;
  88. context.Response.OutputStream.Write(b,0, b.Length);
  89. context.Response.OutputStream.Close();
  90. context.Response.Close();
  91. }
  92. }
  93. }