模拟.net Core ---- http/tcp 通讯-- 发送报文

 1     public class HttpResposeSelf
 2     {
 3         public HttpResposeSelf(int length, Socket st)
 4         {
 5             header = string.Format("Content-Type:text/html;charset=UTF-8
Content-Length:{0}
", length);
 6             this.st = st;
 7         }
 8         public string status { get; set; } //返回状态
 9 
10         public string type { get; set; } // 返回类型
11 
12         public string header { get; set; } // 标头
13 
14         public string content { get; set; } // 正文
15 
16         public Socket st { get; set; }
17 
18 
19         public void SendMessage()
20         {
21             if (string.IsNullOrEmpty(status))
22                 status = "200";
23             byte[] statusline_to_bytes = Encoding.UTF8.GetBytes("HTTP/1.1 " + status + " OK
");
24             st.Send(statusline_to_bytes);  //发送状态行
25             byte[] header_to_bytes = Encoding.UTF8.GetBytes(header);
26             st.Send(header_to_bytes);  //发送应答头
27             st.Send(new byte[] { (byte)'
', (byte)'
' });  //发送空行
28 
29             st.Send(Encoding.UTF8.GetBytes(content));  //发送正文(html)
30             st.Close();
31         }
32     }
View Code
原文地址:https://www.cnblogs.com/duchyaiai/p/8652552.html