Form2.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Printing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Windows.Forms;
  14. namespace UAS_PRINT
  15. {
  16. public partial class Form2 : Form
  17. {
  18. public Form2()
  19. {
  20. InitializeComponent();
  21. }
  22. private static HttpListener httpPostRequest = new HttpListener();
  23. private Thread ThrednHttpPostRequest;
  24. private void Form2_Load(object sender, EventArgs e)
  25. {
  26. httpPostRequest.Prefixes.Add("http://localhost:9100/");
  27. httpPostRequest.Start();
  28. ThrednHttpPostRequest = new Thread(new ThreadStart(httpPostRequestHandle));
  29. ThrednHttpPostRequest.Start();
  30. MyNotifyIcon.ShowBalloonTip(30,"提示","程序启动",ToolTipIcon.Info);
  31. }
  32. private string getRequestPayload(HttpListenerContext req)
  33. {
  34. return "";
  35. }
  36. private static void httpPostRequestHandle()
  37. {
  38. while (true)
  39. {
  40. HttpListenerContext requestContext = httpPostRequest.GetContext();
  41. Thread threadsub = new Thread(new ParameterizedThreadStart((requestcontext) =>
  42. {
  43. HttpListenerContext request = (HttpListenerContext)requestcontext;
  44. //获取Post请求中的参数和值帮助类
  45. HttpListenerPostParaHelper httppost = new HttpListenerPostParaHelper(request);
  46. //获取Post过来的参数和数据
  47. List<HttpListenerPostValue> lst = httppost.GetHttpListenerPostValue();
  48. string userName = "";
  49. string password = "";
  50. string suffix = "";
  51. string adType = "";
  52. //使用方法
  53. //foreach (var key in lst)
  54. //{
  55. // if (key.type == 0)
  56. // {
  57. // string value = Encoding.UTF8.GetString(key.datas).Replace("\r\n", "");
  58. // if (key.name == "username")
  59. // {
  60. // userName = value;
  61. // Console.WriteLine(value);
  62. // }
  63. // if (key.name == "password")
  64. // {
  65. // password = value;
  66. // Console.WriteLine(value);
  67. // }
  68. // if (key.name == "suffix")
  69. // {
  70. // suffix = value;
  71. // Console.WriteLine(value);
  72. // }
  73. // if (key.name == "adtype")
  74. // {
  75. // adType = value;
  76. // Console.WriteLine(value);
  77. // }
  78. // }
  79. // if (key.type == 1)
  80. // {
  81. // string fileName = request.Request.QueryString["FileName"];
  82. // if (!string.IsNullOrEmpty(fileName))
  83. // {
  84. // string filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyMMdd_HHmmss_ffff") + Path.GetExtension(fileName).ToLower();
  85. // if (key.name == "File")
  86. // {
  87. // FileStream fs = new FileStream(filePath, FileMode.Create);
  88. // fs.Write(key.datas, 0, key.datas.Length);
  89. // fs.Close();
  90. // fs.Dispose();
  91. // }
  92. // }
  93. // }
  94. //}
  95. //Response
  96. request.Response.StatusCode = 200;
  97. request.Response.Headers.Add("Access-Control-Allow-Origin", "*");
  98. request.Response.ContentType = "application/json";
  99. requestContext.Response.ContentEncoding = Encoding.UTF8;
  100. string json = "";
  101. if (requestContext.Request.Url.PathAndQuery == "/write")
  102. {
  103. Stream stream = requestContext.Request.InputStream;
  104. var memoryStream = new MemoryStream();
  105. //将基础流写入内存流
  106. const int bufferLength = 10 * 1024 * 1024;
  107. byte[] buffera = new byte[bufferLength];
  108. int actual = stream.Read(buffera, 0, bufferLength);
  109. if (actual > 0)
  110. {
  111. memoryStream.Write(buffera, 0, actual);
  112. }
  113. memoryStream.Position = 0;
  114. string req = System.Text.Encoding.UTF8.GetString(buffera);
  115. PrintHandler.zplprint(req);
  116. }
  117. else if (requestContext.Request.Url.PathAndQuery == "/default?type=printer")
  118. {
  119. PrintDocument PrintDoc = new PrintDocument();
  120. string sDefault = PrintDoc.PrinterSettings.PrinterName;//默认打印机名
  121. //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
  122. json = new JObject(
  123. new JObject(
  124. new JProperty("deviceType", "printer"),
  125. new JProperty("uid", sDefault),
  126. new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
  127. new JProperty("name", sDefault),
  128. new JProperty("connection", "driver"),
  129. new JProperty("version", 2),
  130. new JProperty("manufacturer", "Zebra Technologies")
  131. )
  132. ).ToString();
  133. }
  134. else if (requestContext.Request.Url.PathAndQuery == "/available")
  135. {
  136. List<string> PrintC = new List<string>();
  137. foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
  138. {
  139. PrintC.Add(sPrint);
  140. }
  141. json = new JObject(
  142. new JProperty(
  143. "printer", new JArray
  144. (
  145. from sPrint in PrintC
  146. select new JObject(
  147. new JObject(
  148. new JProperty("deviceType", "printer"),
  149. new JProperty("uid", sPrint),
  150. new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
  151. new JProperty("name", sPrint),
  152. new JProperty("connection", "driver"),
  153. new JProperty("version", 2),
  154. new JProperty("manufacturer", "Zebra Technologies")
  155. )
  156. )
  157. )
  158. )
  159. ).ToString();
  160. }
  161. else
  162. {
  163. json = Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" });
  164. }
  165. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(json);
  166. //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
  167. request.Response.ContentLength64 = buffer.Length;
  168. var output = request.Response.OutputStream;
  169. output.Write(buffer, 0, buffer.Length);
  170. output.Close();
  171. }));
  172. threadsub.Start(requestContext);
  173. }
  174. }
  175. private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  176. {
  177. ThrednHttpPostRequest.Abort();
  178. httpPostRequest.Close();
  179. this.Close();
  180. this.Dispose();
  181. }
  182. }
  183. }