示例#1
0
        private static void DriveHttpListener(HttpListener httpListener, RouteContext routeContext)
        {
            httpListener.BeginGetContext((ar) =>
            {
                var context = httpListener.EndGetContext(ar);
                DriveHttpListener(httpListener, routeContext);

                string path = context.Request.Url.AbsolutePath.Trim().TrimEnd('/');
                var result  = routeContext.Route(path);
                if (result == null)
                {
                    context.Response.StatusCode      = 404;
                    context.Response.ContentLength64 = 0;
                }
                else
                {
                    context.Response.ContentEncoding = Encoding.UTF8;
                    byte[] buffer = Encoding.UTF8.GetBytes(result);
                    context.Response.ContentLength64 = buffer.Length;
                    context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                }
                context.Response.Close();
            }, null);
        }