Form2.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. //Response
  49. request.Response.StatusCode = 200;
  50. request.Response.Headers.Add("Access-Control-Allow-Origin", "*");
  51. request.Response.ContentType = "application/json";
  52. requestContext.Response.ContentEncoding = Encoding.UTF8;
  53. string json = "";
  54. if (requestContext.Request.Url.PathAndQuery == "/write")
  55. {
  56. Stream stream = requestContext.Request.InputStream;
  57. var memoryStream = new MemoryStream();
  58. //将基础流写入内存流
  59. const int bufferLength = 2 * 1024 * 1024;
  60. byte[] buffera = new byte[bufferLength];
  61. int actual = stream.Read(buffera, 0, bufferLength);
  62. if (actual > 0)
  63. {
  64. memoryStream.Write(buffera, 0, actual);
  65. }
  66. memoryStream.Position = 0;
  67. string req = System.Text.Encoding.UTF8.GetString(buffera);
  68. if (req.Contains("\"" + "chooseprintername" + "\""))
  69. {
  70. req = req.Remove(req.IndexOf("device"), req.IndexOf("data") - 2);
  71. PrintHandler.vendorZplPrint(req);
  72. }
  73. else
  74. {
  75. PrintHandler.zplprint(req);
  76. }
  77. }
  78. else if (requestContext.Request.Url.PathAndQuery == "/default?type=printer")
  79. {
  80. PrintDocument PrintDoc = new PrintDocument();
  81. string sDefault = PrintDoc.PrinterSettings.PrinterName;//默认打印机名
  82. //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
  83. json = new JObject(
  84. new JObject(
  85. new JProperty("deviceType", "printer"),
  86. new JProperty("uid", sDefault),
  87. new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
  88. new JProperty("name", sDefault),
  89. new JProperty("connection", "driver"),
  90. new JProperty("version", 2),
  91. new JProperty("manufacturer", "Zebra Technologies")
  92. )
  93. ).ToString();
  94. }
  95. else if (requestContext.Request.Url.PathAndQuery == "/available")
  96. {
  97. List<string> PrintC = new List<string>();
  98. foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
  99. {
  100. PrintC.Add(sPrint);
  101. }
  102. json = new JObject(
  103. new JProperty(
  104. "printer", new JArray
  105. (
  106. from sPrint in PrintC
  107. select new JObject(
  108. new JObject(
  109. new JProperty("deviceType", "printer"),
  110. new JProperty("uid", sPrint),
  111. new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
  112. new JProperty("name", sPrint),
  113. new JProperty("connection", "driver"),
  114. new JProperty("version", 2),
  115. new JProperty("manufacturer", "Zebra Technologies")
  116. )
  117. )
  118. )
  119. )
  120. ).ToString();
  121. }
  122. else
  123. {
  124. json = Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" });
  125. }
  126. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(json);
  127. //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
  128. request.Response.ContentLength64 = buffer.Length;
  129. var output = request.Response.OutputStream;
  130. output.Write(buffer, 0, buffer.Length);
  131. output.Close();
  132. }));
  133. threadsub.Start(requestContext);
  134. }
  135. }
  136. private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
  137. {
  138. ThrednHttpPostRequest.Abort();
  139. httpPostRequest.Close();
  140. this.Close();
  141. this.Dispose();
  142. }
  143. }
  144. }