首页 > 代码库 > 简单的web服务器

简单的web服务器

主要是通过http协议相应用户请求

        public void start()        {            chuantou(80);            using (listerner = new HttpListener())            {                //指定身份验证 Anonymous匿名访问                listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;                string host = common.net.address.get_in_IP();                //if (!string.IsNullOrEmpty(host))                //{                //    listerner.Prefixes.Add("http://" + host + ":80/");                //}                //listerner.Prefixes.Add("http://localhost:80/");                //listerner.Prefixes.Add("http://192.168.1.1:80/");                // listerner.Prefixes.Add("http://localhost/web/");                //任何80端口访问都去处理                listerner.Prefixes.Add("http://+:80/");                listerner.Start();                this.richTextBox1.Text += ("\nWebServer Start Successed.......");                while (true)                {                    //等待请求连接                    //没有请求则GetContext处于阻塞状态                    HttpListenerContext ctx = listerner.GetContext();                    ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码                    ctx.Response.ContentType = "text/html";                    //ctx.Response.ContentEncoding = Encoding.Default;                    string name = ctx.Request.QueryString["name"];                    if (name != null)                    {                        this.richTextBox1.Text += (name);                    }                    //使用Writer输出http响应代码                    using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream, Encoding.UTF8))                    {                        this.richTextBox1.Text += ("\n" + ctx.Request.UserHostAddress);                        //writer.WriteLine("<html><head><title>The WebServer Test</title></head><body>");                        //writer.WriteLine("<div style=\"height:20px;color:blue;text-align:center;\"><p> hello11 {0}</p></div>", name);                        //writer.WriteLine("<ul>");                        ////foreach (string header in ctx.Request.Headers.Keys)                        ////{                        ////    writer.WriteLine("<li><b>{0}:</b>{1}</li>", header, ctx.Request.Headers[header]);                        ////}                        //writer.WriteLine("<li><b>{0}:</b>{1}</li>", ctx.Request.Headers.Keys[4], ctx.Request.Headers[ctx.Request.Headers.Keys[4]]);                        //writer.WriteLine("</ul>");                        //writer.WriteLine("</body></html>");                        //读取文件                        StreamReader reader;                        string readtext = "";                        //string dir = @"D:\server";                        if (ctx.Request.Url.AbsolutePath != "/")                        {                            if (File.Exists(dir + ctx.Request.Url.LocalPath))                            {                                reader = new StreamReader(dir + ctx.Request.Url.LocalPath, Encoding.UTF8);                                //reader = new StreamReader(Path.Combine(dir, ctx.Request.Url.LocalPath.Replace("/", "")), Encoding.UTF8);                                readtext = reader.ReadToEnd();                                reader.Close();                                reader.Dispose();                            }                        }                        else                        {                            reader = new StreamReader(Path.Combine(dir, "index.htm"), Encoding.UTF8);                            readtext = reader.ReadToEnd();                            reader.Close();                            reader.Dispose();                        }                        //writer.Encoding = Encoding.UTF8;                        writer.Write(readtext);                        writer.Close();                        ctx.Response.Close();                    }                }                //listerner.Stop();            }        }

这主要是相应对文本文件的请求,一般的web服务器还需要响应 img css js,

希望大牛指点如何写一个完整的web服务器

 

简单的web服务器