[Visual C#] 异步HttpListener 完全并发处理HTTP请求示例

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Net;
 6 using System.Net.Sockets;
 7 using DevSDK.Net.Sockets;
 8 using System.IO;
 9 
10 namespace ConsoleApplication1
11 {
12     class Program
13     {
14         static HttpListener sSocket = null;
15         
16         static void Main(string[] args)
17         {
18             sSocket = new HttpListener();
19 
20             sSocket.Prefixes.Add("http://127.0.0.1:8080/");
21 
22             sSocket.Start();
23 
24             sSocket.BeginGetContext(new AsyncCallback(GetContextCallBack), sSocket);
25 
26             Console.Read();            
27         }
28 
29         static void GetContextCallBack(IAsyncResult ar)
30         {
31             try
32             {
33                 sSocket = ar.AsyncState as HttpListener;
34 
35                 HttpListenerContext context = sSocket.EndGetContext(ar);
36 
37    sSocket.BeginGetContext(new AsyncCallback(GetContextCallBack), sSocket);
38 
39                 Console.WriteLine(context.Request.Url.PathAndQuery);
40 
41             }
42             catch { }
43             
44         }
45     }
46 }
posted @ 2012-03-07 03:31  star丶清影  阅读(13942)  评论(7编辑  收藏  举报