使用IIS建立自己的網站、使用C#編寫IIS模擬器,更好的理解Client和Server的relation


        如何在IIS服務器上搭建自己的網站呢,今天帶着這問題進行簡單的實踐一下,並且准備模擬一下IIS服務器工作方式,把這個工作方式搞清楚有利於發展。

       1、首先應該進入控制面板=》程序=》添加或刪除程序=》找到IIS 全部打鈎。

  

2、

2、首先要自己建立一個網頁。進行模擬。

3、然后在打開的IIS服務器,點擊右鍵進行建立網頁。

4、然后添加各種配置,如圖:

5、填寫必須的參數如圖:

 

6、點擊OK,點擊瀏覽,如果端口是80端口。不會彈出你所希望看到的窗口。

7、所以我們要進行簡單的調試。

 

8、點擊綁定按鈕:

9:再次在瀏覽器上查看;

到這里一個IIS上布置就基本上完成了。由於時間關系模擬IIS服務器下次給出詳細的代碼,有助你更好的學習網站編程。

先上一個總體的分析圖:

IISDemo

 

                這里給給關注我的好友道個歉,說是補齊下半部分,已經時隔半年,今天終於補齊了這文章,補齊這個文章也引起了我的深思,到你因該怎樣學習,才能成為出色的web開發者。

             IIS模擬思路:

      1. 使用Socket套接字編程。

                 2. 深刻理解web Form 中的 IhttpHandler 、HttpApplication、HttpContext、HttpRequest、HttpResponse 幾個封裝的類。

                 3. 對http協議的有個深刻的認識,或者理解其協議格式。

                 4. 最后就是不錯的編程基礎。把想法寫成代碼。

            

socket編程

           socket編程主要有一下幾步

            

       //獲取ip和端口   
          string host = txthost.Text.Trim();
            int port =int.Parse(txtPort.Text.Trim());
            IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(host), port);
            //建立socket鏈接
            s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Bind(ipe);
            s.Listen(10);
            txtbody.Text = "開始監聽";
           //開始接收,接收時會阻塞,所以采用多進程的方式
            System.Threading.Thread td = new Thread(()=>
            {
                while(true)
                {
                    Socket temp = s.Accept();//為新建立的socket
                   
                    txtbody.Text += "\r\n建立鏈接";
                    string recvStr =string.Empty;//創建接受字符串
                    byte[] recvBytes = new byte[2*1024*1024];
                    int len= temp.Receive(recvBytes, recvBytes.Length, 0);

                    //獲取前台取得的字符串
                    string str = System.Text.Encoding.Default.GetString(recvBytes, 0, len);

                    //解析報文

                    HttpContext context = new HttpContext(str);

                    //處理請求

                    HttpApplication application = new HttpApplication();
                    application.processContext(context);

                    //返回響應

                    temp.Send(context.response.responseHeader());

                    temp.Send(context.response.responsebody);

                    //關閉socket
                    temp.Shutdown(SocketShutdown.Both);
                    temp.Close();

                }

            });
            td.Start();        

    

HttpContext

       字面理解是http上下文。暫且不用去管ms封住的,這里為了演示方面里面封裝了httpRequest,httpResponse屬性。

      

 1 public  class HttpContext
 2     {
 3         public HttpRequest request { get; set; }
 4         public HttpResponse response { get; set; }
 5         public HttpContext(string requestStr)
 6         {
 7             request = new HttpRequest(requestStr);
 8             response = new HttpResponse(request);
 9         }
10     }
View Code

 

HttpRequest

      httpRequest 用於解析Client端的請求,從而告訴context上下文,服務器應該返回怎樣的數據給Client端。

 

 1  public class HttpRequest
 2     {
 3         public string getMothed { get; set; }
 4         public string url { get; set; }
 5         public string version { get; set; }
 6         public HttpRequest( string requestStr)
 7         {
 8             //GET http://c.gj.qq.com HTTP/1.1
 9             //Host: c.gj.qq.com
10             //Connection: keep-alive
11             //Accept: image/webp,image/*,*/*;q=0.8
12             //User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36
13             //Accept-Encoding: gzip, deflate, sdch
14             //Accept-Language: zh-CN,zh;q=0.8
15             //Cookie: pgv_pvid=9224263521; o_cookie=871119239; ptui_loginuin=871119239@qq.com; ptcz=4308290a6e8e29b0645417e52d73567b0bc7ba24b13508b2c685c2a7ccaaec3f; pt2gguin=o0871119239; uin=o0871119239; skey=@Tk5Xvq7l1; qm_username=871119239; qm_sid=0921d209c7585c27d505c5d093cc1d22,qblA2Z21rc3F1eFZBZjFvczRwVypoZXhXelpsU3dQdS0wa2tJQ0l5eWNkSV8.
16             //解析前台發送回來的數據
17             var allLines = requestStr.Replace("\r\n", "\r");
18             //獲取第一行
19             var firstline = allLines.Split('\r')[0];
20             //獲取請求的方法
21             getMothed = firstline.Split(' ')[0];
22             url = firstline.Split(' ')[1];
23             version = firstline.Split(' ')[2];
24         }
25     }
View Code

 

HttpResponse

   httpResponse是返回響應的請求。着返回響應的求請求,必須要處理就需要httpApplicationl.

  

 1  public byte[] responsebody { get; set; }
 2        public HttpRequest request { get; set; }
 3        public HttpResponse(HttpRequest Request)
 4        {
 5            request = Request;
 6        }
 7        public byte[] responseHeader()
 8        {
 9         //HTTP/1.1 200 OK
10         //Date: Sun, 08 May 2016 05:55:33 GMT
11         //Server: Apache
12         //Cache-Control: max-age=0
13         //Expires: Sun, 08 May 2016 05:55:33 GMT
14         //Vary: Accept-Encoding
15         //Content-Length: 40
16         //Connection: close
17         //Content-Type: text/html
18          StringBuilder sb = new StringBuilder();
19          sb.AppendFormat("HTTP/1.1 200 OK\r\n");
20          sb.AppendFormat("Content-Length:{0}\r\n",responsebody.Length);
21          //獲取請求類型
22         var ext=System.IO.Path.GetExtension(request.url);
23         sb.AppendFormat("Content-Type:{0}\r\n\r\n", GetContenType(ext));
24         return System.Text.Encoding.Default.GetBytes(sb.ToString());
25 
26        }
27        public string GetContenType(string ext)
28        {
29            string type = "text/html";
30            switch (ext)
31            {
32                case ".aspx":
33                case ".html":
34                case ".htm":
35                    type = "text/html";
36                    break;
37                case ".png":
38                    type = "image/png";
39                    break;
40                case ".gif":
41                    type = "image/gif";
42                    break;
43                case ".jpg":
44                case ".jpeg":
45                    type = "image/jpeg";
46                    break;
47                case ".css":
48                    type = "text/css";
49                    break;
50                case ".js":
51                    type = "application/x-javascript";
52                    break;
53                default:
54                    type = "text/plain";
55                    break;
56            }
57            return type;
58        }
59     }
View Code

 

 

HttpApplication

  該類繼承接口IhttpHandler

 

 1   public interface IhttpHandler
 2     {
 3        void processContext(HttpContext context);
 4     }
 5  public  class HttpApplication:IhttpHandler
 6     {
 7         public void processContext(HttpContext context)
 8         {
 9             //拼接路徑
10             string basePath = AppDomain.CurrentDomain.BaseDirectory;
11             //虛擬路徑轉絕對路徑
12             string urlPath = context.request.url.TrimStart('/');
13             string path = System.IO.Path.Combine(basePath, urlPath);
14             if (System.IO.File.Exists(path))
15             {
16                 byte[] b = System.IO.File.ReadAllBytes(path);
17                 context.response.responsebody = b;
18             }
19             else
20             {
21                 byte[] b = System.IO.File.ReadAllBytes(basePath +@"/404.html");
22                 context.response.responsebody = b;
23             } 
24 
25         }
26     }
View Code

運行程序即可得到上述發布的網頁。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM