| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using Microsoft.Win32;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Printing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- namespace UAS_PRINT
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
- private static HttpListener httpPostRequest = new HttpListener();
- private Thread ThrednHttpPostRequest;
- private void Form2_Load(object sender, EventArgs e)
- {
- string path = Application.ExecutablePath;
- RegistryKey rk = Registry.LocalMachine;
- RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
- rk2.SetValue("UAS_PRINT.exe", path);
- rk2.Close();
- rk.Close();
- httpPostRequest.Prefixes.Add("http://localhost:9100/");
- httpPostRequest.Start();
- ThrednHttpPostRequest = new Thread(new ThreadStart(httpPostRequestHandle));
- ThrednHttpPostRequest.Start();
- 提示.ShowBalloonTip(30,"提示","程序启动",ToolTipIcon.Info);
- }
- private string getRequestPayload(HttpListenerContext req)
- {
- return "";
- }
- private static void httpPostRequestHandle()
- {
- while (true)
- {
- HttpListenerContext requestContext = httpPostRequest.GetContext();
- Thread threadsub = new Thread(new ParameterizedThreadStart((requestcontext) =>
- {
- HttpListenerContext request = (HttpListenerContext)requestcontext;
- //Response
- request.Response.StatusCode = 200;
- request.Response.Headers.Add("Access-Control-Allow-Origin", "*");
- request.Response.ContentType = "application/json";
- requestContext.Response.ContentEncoding = Encoding.UTF8;
-
- string json = "";
- if (requestContext.Request.Url.PathAndQuery == "/write")
- {
- Stream stream = requestContext.Request.InputStream;
- var memoryStream = new MemoryStream();
- //将基础流写入内存流
- const int bufferLength = 2 * 1024 * 1024;
- byte[] buffera = new byte[bufferLength];
- int actual = stream.Read(buffera, 0, bufferLength);
- if (actual > 0)
- {
- memoryStream.Write(buffera, 0, actual);
- }
- memoryStream.Position = 0;
- string req = System.Text.Encoding.UTF8.GetString(buffera);
- if (req.Contains("\"" + "chooseprintername" + "\""))
- {
- req = req.Remove(req.IndexOf("device"), req.IndexOf("data") - 2);
- lock ("A")
- {
- PrintHandler.vendorZplPrint(req);
- }
- }
- else
- {
- PrintHandler.zplprint(req);
- }
- }
- else if (requestContext.Request.Url.PathAndQuery == "/default?type=printer")
- {
- PrintDocument PrintDoc = new PrintDocument();
- string sDefault = PrintDoc.PrinterSettings.PrinterName;//默认打印机名
- //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
- json = new JObject(
- new JObject(
- new JProperty("deviceType", "printer"),
- new JProperty("uid", sDefault),
- new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
- new JProperty("name", sDefault),
- new JProperty("connection", "driver"),
- new JProperty("version", 2),
- new JProperty("manufacturer", "Zebra Technologies")
- )
- ).ToString();
- }
- else if (requestContext.Request.Url.PathAndQuery == "/available")
- {
- List<string> PrintC = new List<string>();
- foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
- {
- PrintC.Add(sPrint);
- }
- json = new JObject(
- new JProperty(
- "printer", new JArray
- (
- from sPrint in PrintC
- select new JObject(
- new JObject(
- new JProperty("deviceType", "printer"),
- new JProperty("uid", sPrint),
- new JProperty("provider", "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider"),
- new JProperty("name", sPrint),
- new JProperty("connection", "driver"),
- new JProperty("version", 2),
- new JProperty("manufacturer", "Zebra Technologies")
- )
- )
- )
- )
- ).ToString();
- }
- else
- {
- json = Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" });
- }
- byte[] buffer = System.Text.Encoding.UTF8.GetBytes(json);
- //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = "true", msg = "提交成功" }));
- request.Response.ContentLength64 = buffer.Length;
- var output = request.Response.OutputStream;
- output.Write(buffer, 0, buffer.Length);
- output.Close();
-
- }));
- threadsub.Start(requestContext);
- }
- }
- private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Close();
- }
- private void 打开根目录ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory);
- }
- private void Form2_FormClosing(object sender, FormClosingEventArgs e)
- {
- 提示.ShowBalloonTip(30, "提示", "程序关闭", ToolTipIcon.Info);
- if (ThrednHttpPostRequest.IsAlive)
- ThrednHttpPostRequest.Abort();
- if (httpPostRequest.IsListening)
- httpPostRequest.Close();
- Dispose();
- }
- private void Form2_Shown(object sender, EventArgs e)
- {
- Visible = false;
- }
- }
- }
|