HttpListener 實現web服務器


一、使用方法

1. Start()方法 允許此實例接受傳入的請求。即開始監聽

2. Stop()方法 處理完所有當前排隊的請求后關閉HttpListener對象

3. GetContext()方法  等待傳入的請求接受到請求時返回 就如同上一篇的Socket實現服務器一樣 有一個Accept()方法他倆個差不多都是等待傳入的請求還有       一點就是GetContext()方法也會阻塞線程,當客戶端的請求到達時,返回一個HttpListenerContext對象,處理客戶端所發送過來的請求。
    4.1 Request 獲取表示客戶端資源的HttpListenerRequest對象。

          4.1.1 AcceptType 獲取客戶端接受到的MIME類型。
    4.1.2 UserLanguages 獲取語言信息。
          4.1.3 UserAgent 獲取客戶端提供的用戶代理。
          4.1.4 Headers 獲取在請求中發送的標頭名稱/值對的集合 --->獲取HttpListenerRequest類沒有提供的一下屬性。

  4.2 Response 該屬性獲得HttpListenerResponse對象,該對象將被發送到客戶端以響應客戶端的請求。

    4.2.1 ContextLength64 獲取或設置響應中包括的正文數據的字節數。
          4.2.2 ContextType  獲取或設置返回內容的 MIME 類型。

通過流的方式將響應報文體的內容發送給客戶端瀏覽器。

二、源碼

  public void SetWebServer()
        {
            //創建監聽器
            using (var httpListener = new HttpListener())
            {
                //監聽的路徑
                httpListener.Prefixes.Add("http://localhost:8889/");
                //設置匿名訪問
                httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
                //開始監聽
                httpListener.Start();
                while (true)
                {
                    //等待傳入的請求接受到請求時返回,它將阻塞線程,直到請求到達
                    var context = httpListener.GetContext();
                    //取得請求的對象
                    HttpListenerRequest request = context.Request;
                    Console.WriteLine("{0} {1} HTTP/1.1", request.HttpMethod, request.RawUrl);
                    Console.WriteLine("Accept: {0}", string.Join(",", request.AcceptTypes));
                    Console.WriteLine("Accept-Language: {0}",
                        string.Join(",", request.UserLanguages));
                    Console.WriteLine("User-Agent: {0}", request.UserAgent);
                    Console.WriteLine("Accept-Encoding: {0}", request.Headers["Accept-Encoding"]);
                    Console.WriteLine("Connection: {0}",
                        request.KeepAlive ? "Keep-Alive" : "close");
                    Console.WriteLine("Host: {0}", request.UserHostName);
                    Console.WriteLine("Pragma: {0}", request.Headers["Pragma"]);

                    // 取得回應對象
                    HttpListenerResponse response = context.Response;

                    // 設置回應頭部內容,長度,編碼
                    response.ContentEncoding = Encoding.UTF8;
                    response.ContentType = "text/plain;charset=utf-8";

                    var path = @"C:\Users\youziku\Desktop\wendang\";
                    //訪問的文件名
                    var fileName = request.Url.LocalPath;

                    //讀取文件內容
                    var buff = File.ReadAllBytes(path + fileName);
                    response.ContentLength64 = buff.Length;

                    // 輸出回應內容
                    System.IO.Stream output = response.OutputStream;
                    output.Write(buff, 0, buff.Length);
                    // 必須關閉輸出流
                    output.Close();
                }

            }
        }

來源:b̶i̶n̶g̶.̶    與 李二餅

 


免責聲明!

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



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