一段C#代码,模拟IIS功能
   1: using System;
   2: using System.Collections.Generic;
   3: using System.Text;
   4: using System.Net;
   5: using System.IO;
   6:  
   7: namespace HostConsole
   8: {
   9:     class Program
  10:     {
  11:         static void Main(string[] args)
  12:         {
  13:             if (HttpListener.IsSupported)
  14:             {
  15:                 HttpListener listener = new HttpListener();
  16:                 listener.Prefixes.Add("http://+:8080/");
  17:                 listener.Start();
  18:                 while (true)
  19:                 {
  20:                     Console.Write(DateTime.Now.ToString());
  21:                     HttpListenerContext context = listener.GetContext();
  22:                     string page = context.Request.Url.LocalPath.Replace("/","");
  23:                     String query = context.Request.Url.Query.Replace("?", "");
  24:                     StreamReader sr = new StreamReader(context.Request.InputStream);
  25:                     Console.WriteLine(sr.ReadToEnd());
  26:                     Console.WriteLine("接收到请求:{0}?{1}", page, query);
  27:                     StreamWriter sw = new StreamWriter(context.Response.OutputStream);
  28:                     sw.Write("Hello World!");
  29:                     sw.Flush();
  30:                     context.Response.Close();
  31:                 }
  32:             }
  33:         }
  34:     }
  35: }
posted on 2012-06-19 22:28  老劉  阅读(323)  评论(0)    收藏  举报