1     public class HttpResposeSelf
 2     {
 3         public HttpResposeSelf(int length, Socket st)
 4         {
 5             header = string.Format("Content-Type:text/html;charset=UTF-8\r\nContent-Length:{0}\r\n", 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\r\n");
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)'\r', (byte)'\n' });  //发送空行
28 
29             st.Send(Encoding.UTF8.GetBytes(content));  //发送正文(html)
30             st.Close();
31         }
32     }
View Code

 

 posted on 2018-03-26 17:54  一朵茉莉花  阅读(1032)  评论(0编辑  收藏  举报