1£ºÇ°ÑÔ public class HttpServer : IDisposable { private HttpListener listener; public void Start() { listener = new HttpListener(); listener.Prefixes.Add("http://localhost/"); listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication | AuthenticationSchemes.Anonymous; listener.Start(); listener.BeginGetContext(GetContext, null); } private void GetContext(IAsyncResult ar) { HttpListenerRequest Request; HttpListenerResponse Response; try { HttpListenerContext ctx = listener.EndGetContext(ar); Request = ctx.Request; Response = ctx.Response; //setup waiting for the next request listener.BeginGetContext(GetContext, null); } catch (InvalidOperationException) { return; } catch (HttpListenerException) { return; } try { var sw = new StreamWriter(Response.OutputStream); sw.Write(@"<html><body><p>ÄãµÄÇëÇóÒѾ±»½ØÈ¡</p></body></html>"); sw.Flush(); } finally { Response.OutputStream.Flush(); Response.Close(); } } public void Dispose() { if (listener != null) listener.Stop(); } }
|