C # enable the http listener status in the console or winform program,
1 public class THttpListener 2 {3 HttpListener listerner; 4 /// <summary> 5 /// 6 /// </summary> 7 /// <param name = "prefixes"> Format http: /// */test/</param> 8 // <param name = "authent"> </param> 9 public THttpListener (string [] prefixes, AuthenticationSchemes authent = AuthenticationSchemes. anonymous) 10 {11 listerner = new HttpListener (); 12 listerner. authenticationSchemes = authent; // specify the identity to authenticate Anonymous. Anonymous access 13 foreach (var item in prefixes) 14 {15 listerner. prefixes. add (item); 16 Console. writeLine (DateTime. now. toString ("yyyy/MM/dd HH: mm: ss") + ": HttpListener:" + item); 17} 18} 19 20 public delegate void ResponseEventArges (HttpListenerContext ctx ); 21 public event ResponseEventArges ResponseEvent; 22 AsyncCallback ac = null; 23 24 public void Start () 25 {26 if (! Listerner. isListening) 27 {28 listerner. start (); 29 ac = new AsyncCallback (GetContextAsyncCallback); 30 listerner. beginGetContext (ac, null); 31 Console. writeLine (DateTime. now. toString ("yyyy/MM/dd HH: mm: ss") + ": Start "); 32} 33} 34 35 // <summary> 36 // Stop listening service 37 // </summary> 38 public void Stop () 39 {40 listerner. stop (); 41} 42 43 // <summary> 44 // receives the listener request callback 45 /// </summary> 46 // <p Aram name = "ia"> </param> 47 public void GetContextAsyncCallback (IAsyncResult ia) 48 {49 if (ia. isCompleted) 50 {51 var ctx = listerner. endGetContext (ia); 52 ctx. response. statusCode = 200; 53 if (ResponseEvent! = Null) 54 {55 ResponseEvent. beginInvoke (ctx, null, null); 56} 57 else 58 {59 System. IO. binaryWriter br = new System. IO. binaryWriter (ctx. response. outputStream, new UTF8Encoding (); 60 br. write ("error: Server not processed"); 61 ctx. response. close (); 62 br. close (); 63 br. dispose (); 64} 65} 66 listerner. beginGetContext (ac, null); 67} 68 69 public Dictionary <string, string> getData (System. net. httpListenerContext ctx) 70 {71 var request = ctx. request; 72 if (request. httpMethod = "GET") 73 {74 return getData (ctx, DataType. get); 75} 76 else 77 {78 return getData (ctx, DataType. post); 79} 80} 81 82 public Dictionary <string, string> getData (System. net. httpListenerContext ctx, DataType type) 83 {84 var rets = new Dictionary <string, string> (); 85 var request = ctx. request; 86 switch (type) 87 {88 case DataType. post: 89 if (request. httpMethod = "POST") 90 {91 string rawData; 92 using (var reader = new StreamReader (request. inputStream, request. contentEncoding) 93 {94 rawData = reader. readToEnd (); 95} 96 string [] rawParams = rawData. split ('&'); 97 foreach (string param in rawParams) 98 {99 string [] kvPair = param. split ('='); 100 string key = kvPair [0]; 101 string value = HttpUtility. urlDecode (kvPair [1]); 102 rets [key] = value; 103} 104} 105 break; 106 case DataType. get: 107 if (request. httpMethod = "GET") 108 {109 string [] keys = request. queryString. allKeys; 110 foreach (string key in keys) 111 {112 rets [key] = request. queryString [key]; 113} 114} 115 break; 116} 117 return rets; 118} 119 120 // <summary> 121 // data submission method 122 // </summary> 123 public enum DataType124 {125 Post, 126 Get, 127} 128 129 130}
Test call class
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 // get data directly 15 Dictionary <string, string> rets = _ HttpListener. getData (ctx); 16 // get data 17 Dictionary <string, string> retGets = _ HttpListener. getData (ctx, THttpListener. dataType. get); 18 // Get the post data 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 // use Writer to output http response code 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}
When we need to enable http listening in the service program or background program to obtain the submitted data, but it cannot be linked by the web server,