/// <summary> /// 监听 /// </summary> public void listen() { IPAddress MyIP = IPAddress.Parse("0"); listener = new TcpListener(MyIP, port); listener.Start(); while (is_active) { TcpClient s = listener.AcceptTcpClient(); UnHttpServerProcessor processor = new UnHttpServerProcessor(s, this); Thread thread = new Thread(new ThreadStart(processor.process)); thread.Start(); Thread.Sleep(1); } }
/// <summary> /// POST句柄 /// </summary> /// <param name="p"></param> /// <param name="inputData"></param> public abstract void handlePOSTRequest(UnHttpServerProcessor p, StreamReader inputData);
/// <summary> /// POST处理 /// </summary> /// <param name="p"></param> /// <param name="inputData"></param> public override void handlePOSTRequest(UnHttpServerProcessor p, StreamReader inputData) { //Console.WriteLine("POST request: {0}", p.http_url); string data = inputData.ReadToEnd(); Console.WriteLine(data); p.outputStream.WriteLine("<html><body><h1>test server</h1>"); p.outputStream.WriteLine("<a href=/test>return</a><p>"); p.outputStream.WriteLine("postbody: <pre>{0}</pre>", data); }
/// <summary> /// GET句柄 /// </summary> /// <param name="p"></param> public abstract void handleGETRequest(UnHttpServerProcessor p);
/// <summary> /// GET处理 /// </summary> /// <param name="p"></param> public override void handleGETRequest(UnHttpServerProcessor p) { //Console.WriteLine("request: {0}", p.http_url); p.writeSuccess(); p.outputStream.WriteLine("<html><body><h1>test server</h1>"); p.outputStream.WriteLine("Current Time: " + DateTime.Now.ToString()); p.outputStream.WriteLine("url : {0}", p.http_url); p.outputStream.WriteLine("<form method=post action=/form>"); p.outputStream.WriteLine("<input type=text name=foo value=foovalue>"); p.outputStream.WriteLine("<input type=submit name=bar value=barvalue>"); p.outputStream.WriteLine("</form>"); }