HttpResponse.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace ServerLib
  8. {
  9. public class HttpResponse : BaseHeader
  10. {
  11. public string StatusCode { get; set; }
  12. public string Protocols { get; set; }
  13. public string ProtocolsVersion { get; set; }
  14. public byte[] Content { get; private set; }
  15. private Stream handler;
  16. public ILogger Logger { get; set; }
  17. public HttpResponse(Stream stream)
  18. {
  19. this.handler = stream;
  20. this.Headers = new Dictionary<string, string>();
  21. }
  22. public HttpResponse SetContent(byte[] content, Encoding encoding = null)
  23. {
  24. this.Content = content;
  25. this.Encoding = encoding != null ? encoding : Encoding.UTF8;
  26. this.Content_Length = content.Length.ToString();
  27. return this;
  28. }
  29. public HttpResponse SetContent(string content, Encoding encoding = null)
  30. {
  31. //初始化内容
  32. encoding = encoding != null ? encoding : Encoding.UTF8;
  33. return SetContent(encoding.GetBytes(content), encoding);
  34. }
  35. public Stream GetResponseStream()
  36. {
  37. return this.handler;
  38. }
  39. public string GetHeader(ResponseHeaders header)
  40. {
  41. return GetHeaderByKey(header);
  42. }
  43. public string GetHeader(string fieldName)
  44. {
  45. return GetHeaderByKey(fieldName);
  46. }
  47. public void SetHeader(ResponseHeaders header, string value)
  48. {
  49. SetHeaderByKey(header, value);
  50. }
  51. public void SetHeader(string fieldName, string value)
  52. {
  53. SetHeaderByKey(fieldName, value);
  54. }
  55. /// <summary>
  56. /// 构建响应头部
  57. /// </summary>
  58. /// <returns></returns>
  59. protected string BuildHeader()
  60. {
  61. StringBuilder builder = new StringBuilder();
  62. if (!string.IsNullOrEmpty(StatusCode))
  63. builder.Append("HTTP/1.1 " + StatusCode + "\r\n");
  64. if (!string.IsNullOrEmpty(this.Content_Type))
  65. builder.AppendLine("Content-Type:" + this.Content_Type);
  66. return builder.ToString();
  67. }
  68. /// <summary>
  69. /// 发送数据
  70. /// </summary>
  71. public void Send()
  72. {
  73. if (!handler.CanWrite) return;
  74. try
  75. {
  76. //发送响应头
  77. var header = BuildHeader();
  78. byte[] headerBytes = this.Encoding.GetBytes(header);
  79. handler.Write(headerBytes, 0, headerBytes.Length);
  80. //发送空行
  81. byte[] lineBytes = this.Encoding.GetBytes(System.Environment.NewLine);
  82. handler.Write(lineBytes, 0, lineBytes.Length);
  83. //发送内容
  84. handler.Write(Content, 0, Content.Length);
  85. }
  86. catch (Exception e)
  87. {
  88. Log(e.Message);
  89. }
  90. finally
  91. {
  92. handler.Close();
  93. }
  94. }
  95. /// <summary>
  96. /// 记录日志
  97. /// </summary>
  98. /// <param name="message">日志消息</param>
  99. private void Log(object message)
  100. {
  101. if (Logger != null)
  102. Logger.Log(message);
  103. }
  104. }
  105. }