用到的類主要有HttpListener、StreamWriter.
HttpListener:使用HttpListener可創建響應 HTTP
請求的簡單 HTTP
協議偵聽器。實際上HttpListener只是實現了服務器端Socket上面的一個簡單封裝類。通過設置Prefixes屬性來進行偵聽,如,偵聽器綁定到http或https端點的URL(如下代碼).偵聽器默認是綁定到運行在http的80端口和https的443的端口,並允許匿名和無身份驗證的客戶端訪問。可以使用HttpListener類的屬性Prefixes來制定自定義url和端口,並通過設置屬性AuthenticationSchemes屬性來配置身份驗證。
建立HttpListener后,執行Start(執行是Start實際上是引發底層的Bind和Listener),就開始處理客服端輸入請求。HttpListener中GetContext和套接字的Accept類似,它在沒有請求准備好處理的時候處於被阻塞狀態。GetContext返回一個對象HttpListenerContext。HttpListenerContext對象的屬性Request屬性提供了許多關於客戶機的一些請求信息.Response則返回一個HttpListerResponse對象,該對象可以用來響應客戶機的請求。
代碼如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 using System.Net; 6 using System.Net.Sockets; 7 using System.IO; 8 9 namespace ConsoleHttpEchoServer 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 using (HttpListener listerner = new HttpListener()) 16 { 17 listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份驗證 Anonymous匿名訪問 18 listerner.Prefixes.Add("http://localhost:8080/web/"); 19 20 // listerner.Prefixes.Add("http://localhost/web/"); 21 listerner.Start(); 22 Console.WriteLine("WebServer Start Successed......."); 23 while (true) 24 { 25 //等待請求連接 26 //沒有請求則GetContext處於阻塞狀態 27 HttpListenerContext ctx = listerner.GetContext(); 28 ctx.Response.StatusCode = 200;//設置返回給客服端http狀態代碼 29 string name = ctx.Request.QueryString["name"]; 30 31 if (name != null) 32 { 33 Console.WriteLine(name); 34 } 35 36 37 //使用Writer輸出http響應代碼 38 using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream)) 39 { 40 Console.WriteLine("hello"); 41 writer.WriteLine("<html><head><title>The WebServer Test</title></head><body>"); 42 writer.WriteLine("<div style=\"height:20px;color:blue;text-align:center;\"><p> hello {0}</p></div>", name); 43 writer.WriteLine("<ul>"); 44 45 foreach (string header in ctx.Request.Headers.Keys) 46 { 47 writer.WriteLine("<li><b>{0}:</b>{1}</li>", header, ctx.Request.Headers[header]); 48 49 } 50 writer.WriteLine("</ul>"); 51 writer.WriteLine("</body></html>"); 52 53 writer.Close(); 54 ctx.Response.Close(); 55 } 56 57 } 58 listerner.Stop(); 59 } 60 } 61 } 62 }
在瀏覽器 中輸入 http://localhost:8080/web/?name=test或
http://localhost/web/?name=test,在瀏覽器就會出現Hello test
和一些Request頭部相關信息.
備注:
1.URI 前綴字符串由方案(http 或
https)、主機、可選端口和可選路徑組成。完整前綴字符串的示例為“http://www.contoso.com:8080/customerData/”。前綴必須以正斜杠(“/”)結尾。帶有與所請求的
URI 最近似匹配的前綴的 HttpListener 對象響應請求。多個 HttpListener 對象不能添加同一前綴;如果 HttpListener
添加已經使用的前綴,將引發Win32Exception 異常。
2.如果指定了端口,主機元素可以被替換為“*”,以指示如果請求的
URI 與任何其他前綴都不匹配,則 HttpListener 接受發送到該端口的請求。例如,當任何 HttpListener 都不處理請求的 URI
時,若要接收發送到端口 8080 的所有請求,前綴應為“http://*:8080/”。同樣,若要指定 HttpListener
接受發送到端口的所有請求,請將主機元素替換為“+”字符,即“https://+:8080”。“*”和“+”字符可出現在包含路徑的前綴中。
3.AuthenticationSchemes枚舉類有以下值
Anonymous:默認值,允許任意的客戶機進行連接,不需要身份驗證。
Basic:要求提供純文本(64位編碼)用戶名和密碼來進行驗證。
Digest:類似與基本身份驗證,但是用戶名和密碼使用一個簡單密碼散列進行傳輸。
Ntlm: 指定 NTLM
身份驗證(只有IE支持)。
IntegratedWindowsAuthentication:指定Windows身份驗證。
Negotiate: 和客戶端協商以確定身份驗證方案。如果客戶端和服務器均支持 Kerberos,則使用 Kerberos;否則使用
Ntlm
None:不進行身份驗證。