一段C#代码,模拟IIS功能。

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace HostConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            if (HttpListener.IsSupported)
            {
                HttpListener listener = new HttpListener();
                listener.Prefixes.Add("http://+:8080/");
                listener.Start();
                while (true)
                {
                    Console.Write(DateTime.Now.ToString());
                    HttpListenerContext context = listener.GetContext();
                    string page = context.Request.Url.LocalPath.Replace("/","");
                    String query = context.Request.Url.Query.Replace("?", "");
                    StreamReader sr = new StreamReader(context.Request.InputStream);
                    Console.WriteLine(sr.ReadToEnd());
                    Console.WriteLine("接收到请求:{0}?{1}", page, query);
                    StreamWriter sw = new StreamWriter(context.Response.OutputStream);
                    sw.Write("Hello World!");
                    sw.Flush();
                    context.Response.Close();
                }
            }
        }
    }
}

  

posted @ 2012-03-29 23:20  Soar、毅  阅读(991)  评论(1编辑  收藏  举报