/// <summary> /// 处理Post请求; /// </summary> /// <param name="p"></param> /// <param name="inputStreamReader"></param> public override void HandlePostRequest(HttpProcessor processor, StreamReader inputStreamReader) { Console.WriteLine("POST request: {0}", processor.m_HttpUrl); string data = inputStreamReader.ReadToEnd(); processor.m_OutputStreamWriter.WriteLine("<html><body><h1>test server</h1>"); processor.m_OutputStreamWriter.WriteLine("<a href=/test>return</a><p>"); processor.m_OutputStreamWriter.WriteLine("postbody: <pre>{0}</pre>", data); }
/// <summary> /// 处理Get请求; /// </summary> /// <param name="p"></param> public override void HandleGetRequest(HttpProcessor processor) { Console.WriteLine("request: {0}", processor.m_HttpUrl); processor.WriteSuccess(); processor.m_OutputStreamWriter.WriteLine("<html><body><h1>LightHttpServer</h1>"); processor.m_OutputStreamWriter.WriteLine("Current Time: " + DateTime.Now.ToString()); processor.m_OutputStreamWriter.WriteLine("url : {0}", processor.m_HttpUrl); //Post测试; processor.m_OutputStreamWriter.WriteLine("<form method=post action=/form>"); processor.m_OutputStreamWriter.WriteLine("<input type=text name=foo value=foovalue>"); processor.m_OutputStreamWriter.WriteLine("<input type=submit name=bar value=barvalue>"); processor.m_OutputStreamWriter.WriteLine("</form>"); }
public void StartListen() { m_TcpListener = new TcpListener(IPAddress.Parse(m_IpAddress), m_Port); //开始监听,这里有重载可以设置最大挂起数; m_TcpListener.Start(); while (m_isActive) { //等待客户端连接; TcpClient tcpClient = m_TcpListener.AcceptTcpClient(); //客户端连接之后,开始对连接数据进行处理; HttpProcessor processor = new HttpProcessor(tcpClient, this); processor.Process(); } }
/// <summary> /// 处理Post操作; /// </summary> /// <param name="p"></param> /// <param name="inputStreamReader"></param> public abstract void HandlePostRequest(HttpProcessor p, StreamReader inputStreamReader);
/// <summary> /// 处理Get操作; /// </summary> /// <param name="p"></param> public abstract void HandleGetRequest(HttpProcessor p);