Form2.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Microsoft.Win32;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Drawing.Printing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Windows.Forms;
  15. namespace UAS_PRINT
  16. {
  17. public partial class Form2 : Form
  18. {
  19. public Form2()
  20. {
  21. InitializeComponent();
  22. }
  23. private static HttpListener httpPostRequest = new HttpListener();
  24. private Thread ThrednHttpPostRequest;
  25. private void Form2_Load(object sender, EventArgs e)
  26. {
  27. string path = Application.ExecutablePath;
  28. RegistryKey rk = Registry.LocalMachine;
  29. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  30. rk2.SetValue("UAS_PRINT.exe", path);
  31. rk2.Close();
  32. rk.Close();
  33. httpPostRequest.Prefixes.Add("http://localhost:9100/");
  34. httpPostRequest.Start();
  35. ThrednHttpPostRequest = new Thread(new ThreadStart(httpPostRequestHandle));
  36. ThrednHttpPostRequest.Start();
  37. MyNotifyIcon.ShowBalloonTip(30,"提示","程序启动",ToolTipIcon.Info);
  38. }
  39. private string getRequestPayload(HttpListenerContext req)
  40. {
  41. return "";
  42. }
  43. private static void httpPostRequestHandle()
  44. {
  45. while (true)
  46. {
  47. HttpListenerContext requestContext = httpPostRequest.GetContext();
  48. Thread threadsub = new Thread(new ParameterizedThreadStart((requestcontext) =>
  49. {
  50. HttpListenerContext request = (HttpListenerContext)requestcontext;
  51. //获取Post请求中的参数和值帮助类
  52. HttpListenerPostParaHelper httppost = new HttpListenerPostParaHelper(request);
  53. //获取Post过来的参数和数据
  54. List<HttpListenerPostValue> lst = httppost.GetHttpListenerPostValue();
  55. //Response
  56. request.Response.StatusCode = 200;
  57. request.Response.Headers.Add("Access-Control-Allow-Origin", "*");
  58. request.Response.ContentType = "application/json";
  59. requestContext.Response.ContentEncoding = Encoding.UTF8;
  60. string json = "";
  61. if (requestContext.Request.Url.PathAndQuery == "/write")
  62. {
  63. Stream stream = requestContext.Request.InputStream;
  64. var memoryStream = new MemoryStream();
  65. //将基础流写入内存流
  66. const int bufferLength = 2 * 1024 * 1024;
  67. byte[] buffera = new byte[bufferLength];
  68. int actual = stream.Read(buffera, 0, bufferLength);
  69. if (actual > 0)
  70. {
  71. memoryStream.Write(buffera, 0, actual);
  72. }
  73. memoryStream.Position = 0;
  74. string req = System.Text.Encoding.UTF8.GetString(buffera);
  75. if (req.Contains("\"" + "chooseprintername" + "\""))
  76. {
  77. req = req.Remove(req.IndexOf("device"), req.IndexOf("data") - 2);
  78. PrintHandler.vendorZplPrint(req);
  79. }
  80. else
  81. {
  82. PrintHandler.zplprint(req);
  83. }
  84. }
  85. else if (requestContext.Request.Url.PathAndQuery == "/default?type=printer")
  86. {
  87. PrintDocument PrintDoc = new PrintDocument();
  88. string sDefault = PrintDoc.PrinterSettings.PrinterName;//默认打印机名
  89. //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
  90. json = new JObject(
  91. new JObject(
  92. new JProperty("deviceType", "printer"),
  93. new JProperty("uid", sDefault),
  94. new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
  95. new JProperty("name", sDefault),
  96. new JProperty("connection", "driver"),
  97. new JProperty("version", 2),
  98. new JProperty("manufacturer", "Zebra Technologies")
  99. )
  100. ).ToString();
  101. }
  102. else if (requestContext.Request.Url.PathAndQuery == "/available")
  103. {
  104. List<string> PrintC = new List<string>();
  105. foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
  106. {
  107. PrintC.Add(sPrint);
  108. }
  109. json = new JObject(
  110. new JProperty(
  111. "printer", new JArray
  112. (
  113. from sPrint in PrintC
  114. select new JObject(
  115. new JObject(
  116. new JProperty("deviceType", "printer"),
  117. new JProperty("uid", sPrint),
  118. new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
  119. new JProperty("name", sPrint),
  120. new JProperty("connection", "driver"),
  121. new JProperty("version", 2),
  122. new JProperty("manufacturer", "Zebra Technologies")
  123. )
  124. )
  125. )
  126. )
  127. ).ToString();
  128. }
  129. else
  130. {
  131. json = Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" });
  132. }
  133. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(json);
  134. //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
  135. request.Response.ContentLength64 = buffer.Length;
  136. var output = request.Response.OutputStream;
  137. output.Write(buffer, 0, buffer.Length);
  138. output.Close();
  139. }));
  140. threadsub.Start(requestContext);
  141. }
  142. }
  143. private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  144. {
  145. ThrednHttpPostRequest.Abort();
  146. httpPostRequest.Close();
  147. this.Close();
  148. this.Dispose();
  149. }
  150. }
  151. }