self_http_manager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.Eventing.Reader;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace HXX.Scanner.Biz
  12. {
  13. public class self_http_manager
  14. {
  15. private static self_http_manager _instance;
  16. private static string HostURL = "http://localhost:9998/";
  17. public static self_http_manager Instance
  18. {
  19. get
  20. {
  21. if (_instance == null)
  22. {
  23. _instance = new self_http_manager();
  24. }
  25. return _instance;
  26. }
  27. }
  28. private System.Net.HttpListener _listener = null;
  29. public bool startSta = false;
  30. /// <summary>
  31. /// 启动
  32. /// </summary>
  33. //public void Start(string ip, int port)
  34. public void Start()
  35. {
  36. Stop();
  37. List<string> httpPrefixes = new List<string>();
  38. //httpPrefixes.Add("http://" + ip + ":" + port + "/" + "UpVmsRecord/");
  39. //httpPrefixes.Add("http://" + ip + ":" + port + "/");
  40. httpPrefixes.Add(HostURL);
  41. new Thread(new ThreadStart(delegate
  42. {
  43. _listener = new HttpListener();
  44. while (true)
  45. {
  46. try
  47. {
  48. _listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
  49. //_listener.Prefixes.Add(httpPrefixes0);
  50. //_listener.Prefixes.Add(httpPrefixes1);
  51. if (httpPrefixes != null)
  52. {
  53. foreach (string url in httpPrefixes)
  54. {
  55. _listener.Prefixes.Add(url);
  56. }
  57. }
  58. _listener.Start();
  59. }
  60. catch (Exception ex)
  61. {
  62. startSta = false;
  63. break;
  64. }
  65. //线程池
  66. int minThreadNum;
  67. int portThreadNum;
  68. int maxThreadNum;
  69. ThreadPool.GetMaxThreads(out maxThreadNum, out portThreadNum);
  70. ThreadPool.GetMinThreads(out minThreadNum, out portThreadNum);
  71. //ThreadPool.QueueUserWorkItem(new WaitCallback(TaskProc1), x);
  72. try
  73. {
  74. while (true)
  75. {
  76. startSta = true;
  77. //等待请求连接
  78. //没有请求则GetContext处于阻塞状态
  79. HttpListenerContext ctx = _listener.GetContext();
  80. ThreadPool.QueueUserWorkItem(new WaitCallback(TaskProc), ctx);
  81. }
  82. }
  83. catch
  84. {
  85. startSta = false;
  86. }
  87. }
  88. })).Start();
  89. }
  90. /// <summary>
  91. /// 停止
  92. /// </summary>
  93. public void Stop()
  94. {
  95. if (_listener != null)
  96. {
  97. _listener.Stop();
  98. _listener.Close();
  99. _listener = null;
  100. }
  101. }
  102. /// <summary>
  103. /// 任务进
  104. /// </summary>
  105. /// <param name="obj"></param>
  106. void TaskProc(object obj)
  107. {
  108. HttpListenerContext ctx = (HttpListenerContext)obj;
  109. try
  110. {
  111. var url = ctx.Request.Url.AbsoluteUri;
  112. //Stream stream = ctx.Request.InputStream;
  113. //System.IO.StreamReader reader = new System.IO.StreamReader(stream, Encoding.UTF8);
  114. //if (url.Contains("UpVmsRecord"))
  115. //{
  116. // string body = reader.ReadToEnd();
  117. // ////这里的body就是客户端发过来的数据
  118. // //var upRecord = Newtonsoft.Json.JsonConvert.DeserializeObject<UpEventRecord>(body);
  119. // //if (upRecord != null)
  120. // //{
  121. // // Form1._Instance.InsertRecord(upRecord);
  122. // //}
  123. //}
  124. //stream.Close();
  125. ctx.Response.AddHeader("Access-Control-Allow-Origin", "*");
  126. ctx.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
  127. ctx.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type,Authorization,X-Requested-With");
  128. ctx.Response.AddHeader("Access-Control-Max-Age", "3600");
  129. var bytes = bizWork(url);
  130. ctx.Response.OutputStream.Write(bytes, 0, bytes.Length);
  131. ctx.Response.Close();
  132. ctx = null;
  133. }
  134. catch (Exception ex)
  135. {
  136. System.Console.WriteLine(ex.ToString());
  137. }
  138. }
  139. private byte[] bizWork(string url)
  140. {
  141. byte[] bytes;
  142. try
  143. {
  144. var path = url.Replace(HostURL, "");
  145. path = config_manager.Get("dataDir") + "/" + path;
  146. if (File.Exists(path))
  147. {
  148. using (FileStream fileStream = new FileStream(path, FileMode.Open))
  149. {
  150. bytes = new byte[fileStream.Length];
  151. fileStream.Read(bytes, 0, bytes.Length);
  152. }
  153. }
  154. else
  155. {
  156. bytes = Encoding.UTF8.GetBytes("File Not Found");
  157. }
  158. }
  159. catch (Exception ee)
  160. {
  161. bytes = Encoding.UTF8.GetBytes(ee.Message);
  162. }
  163. return bytes;
  164. }
  165. public static string get_url(string file_full_name)
  166. {
  167. var result = HostURL + file_full_name.Replace(config_manager.Get("dataDir"), "").Replace(@"\", "/");
  168. return result;
  169. }
  170. }
  171. }