Proce***equestHandler(page,query,TextWriterwriter);WebServer:MarshalByRefObject,IRegisteredObject{Proce***equest(page,query,TextWriterwriter){SimpleWorkerRequestworker=SimpleWorkerRequest(page,query,writer);HttpRuntime.Proce***equest(worker);}Stop(immediate){HostingEnvironment.UnregisterObject();}}SimpleHttpListener{Proce***equestHandlerProce***equest;SetProce***equest(Proce***equestHandlerhandler){.Proce***equest=handler;}PhysicRoot=.Empty;publicvoidRun(){if(!HttpListener.IsSupported)thrownewInvalidOperationException("使用HttpListener必须为WindowsXPSP2或Server2003以上系统!");string[]prefixes=newstring[]{"http://localhost:49152/"};HttpListenerlistener=newHttpListener();foreach(stringiteminprefixes){listener.Prefixes.Add(item);}listener.Start();Console.WriteLine("开始监听.....");while(true){HttpListenerContextcontext=listener.GetContext();HttpListenerRequestrequest=context.Request;Console.WriteLine("{0}{1}HTTP/1.1",request.HttpMethod,request.RawUrl);HttpListenerResponseresponse=context.Response;using(StreamWriterwriter=newStreamWriter(response.OutputStream)){StringWritersw=newStringWriter();stringcontent=string.Empty;stringfilename=request.Url.LocalPath.ToLower();Regexreg=newRegex("(.aspx|.ashx|.svc|.asmx)");Matchm=reg.Match(filename);if(m.Success){stringpath=request.Url.AbsolutePath;stringquery=request.Url.Query.Replace("?",string.Empty);stringprefx=m.Groups[0].Value;if(!filename.EndsWith(prefx)){intindex=path.IndexOf(prefx);index=index+prefx.Length;query=path.Substring(index);path=path.Substring(0,index);}path=Path.GetFileName(path);this.Proce***equest(path,query,sw);content=sw.ToString();sw.Close();response.ContentLength74=Encoding.UTF8.GetByteCount(content);response.ContentType="text/html;charset=UTF-8";writer.Write(content);}else{stringfilePath=Path.Combine(PhysicRoot,request.Url.AbsolutePath.Substring(1));lock(this){byte[]buffer=newbyte[4096];if(File.Exists(filePath)){using(StreamReadersr=newStreamReader(filePath)){response.ContentLength74=sr.BaseStream.Length;intreadLength=sr.BaseStream.Read(buffer,0,buffer.Length);while(readLength>0){writer.BaseStream.Write(buffer,0,readLength);readLength=sr.BaseStream.Read(buffer,0,buffer.Length);}}}else{stringmsg="notfound:"+filePath;byte[]data=Encoding.UTF8.GetBytes(msg);writer.BaseStream.Write(data,0,data.Length);}}}Console.WriteLine("\r\nProcessOk.\r\n");}if(Console.KeyAvailable)break;}listener.Stop();}Start(){PhysicRoot=;WebServerserver=CreateWorkerAppDomainWithHost((WebServer),,PhysicRoot)WebServer;SimpleHttpListenerlistener=SimpleHttpListener();listener.SetProce***equest(server.Proce***equest);listener.Run();}CreateWorkerAppDomainWithHost(TypehostType,virtualPath,physicalPath){uniqueAppString=.Concat(virtualPath,physicalPath).ToLowerInvariant();appid=(uniqueAppString.GetHashCode()).ToString(,CultureInfo.InvariantCulture);appmanager=ApplicationManager.GetApplicationManager();buildManagerHostType=(HttpRuntime).Assembly.GetType();buildManagerHost=appmanager.CreateObject(appid,buildManagerHostType,virtualPath,physicalPath,);buildManagerHostType.InvokeMember(,BindingFlags.Instance|BindingFlags.InvokeMethod|BindingFlags.NonPublic,,buildManagerHost,[]{hostType.Assembly.FullName,hostType.Assembly.Location});appmanager.CreateObject(appid,hostType,virtualPath,physicalPath,);}}