1 public class THttpListener 2 { 3 HttpListener listerner; 4 /// <summary> 5 /// 6 /// </summary> 7 /// <param name="測試調用類
1 public class TestHttp 2 { 3 THttpListener _HttpListener; 4 public TestHttp() 5 { 6 string[] strUrl = new string[] { "http://*/Test/" }; 7 _HttpListener = new THttpListener(strUrl); 8 _HttpListener.ResponseEvent += _HttpListener_ResponseEvent; 9 _HttpListener.Start();10 }11 12 void _HttpListener_ResponseEvent(System.Net.HttpListenerContext ctx)13 {14 //直接獲取數據15 Dictionary<string, string> rets = _HttpListener.getData(ctx);16 //獲取get數據17 Dictionary<string, string> retGets = _HttpListener.getData(ctx, THttpListener.DataType.Get);18 //獲取post數據19 Dictionary<string, string> retPosts = _HttpListener.getData(ctx, THttpListener.DataType.Post);20 ResponseWrite(ctx.Request.AcceptTypes[0], "Ret", ctx.Response);21 }22 23 static void ResponseWrite(string type, string msg, System.Net.HttpListenerResponse response)24 {25 //使用Writer輸出http響應代碼26 using (System.IO.StreamWriter writer = new System.IO.StreamWriter(response.OutputStream, new UTF8Encoding()))27 {28 response.ContentType = type + ";charset=utf-8";29 writer.WriteLine(msg);30 writer.Close();31 response.Close();32 }33 }34 }當我們在服務程序或者后臺程序需要開啟對http的監聽,來獲取提交數據,但是又不能web服務器來掛在的程序,
新聞熱點
疑難解答