世間萬物 只有想不到 沒有作不到 哈哈哈 仔細閱讀代碼 我要湊夠 150個字 哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈html
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Net; 6 using System.IO; 7 8 namespace ConsoleApplication1 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 15 using (HttpListener listerner = new HttpListener()) 16 { 17 listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份驗證 Anonymous匿名訪問 18 listerner.Prefixes.Add("http://localhost:8080/web/"); 19 listerner.Start(); 20 Console.WriteLine("WebServer Start Successed......."); 21 while (true) 22 { 23 //等待請求鏈接 24 //沒有請求則GetContext處於阻塞狀態 25 HttpListenerContext ctx = listerner.GetContext(); 26 ctx.Response.StatusCode = 200;//設置返回給客服端http狀態代碼 27 string name = ctx.Request.QueryString["name"]; 28 29 if (name != null) 30 { 31 Console.WriteLine(name); 32 } 33 34 35 //使用Writer輸出http響應代碼 36 using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream)) 37 { 38 Console.WriteLine("hello"); 39 writer.WriteLine("<html><head><title>The WebServer Test</title></head><body>"); 40 writer.WriteLine("<div style=\"height:20px;color:blue;text-align:center;\"><p> hello {0}</p></div>", name); 41 writer.WriteLine("<ul>"); 42 43 foreach (string header in ctx.Request.Headers.Keys) 44 { 45 writer.WriteLine("<li><b>{0}:</b>{1}</li>", header, ctx.Request.Headers[header]); 46 47 } 48 writer.WriteLine("</ul>"); 49 writer.WriteLine("</body></html>"); 50 51 writer.Close(); 52 ctx.Response.Close(); 53 } 54 55 } 56 listerner.Stop(); 57 } 58 } 59 60 61 62 } 63 }
簡不簡單 明不明瞭 哈哈哈 低調 web