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; using UAS_PRINT.Properties; 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.Prefixes.Add("https://localhost:9100/"); httpPostRequest.Start(); ThrednHttpPostRequest = new Thread(new ThreadStart(httpPostRequestHandle)); ThrednHttpPostRequest.Start(); 提示.ShowBalloonTip(30, "提示", "程序启动", ToolTipIcon.Info); FTPtoolStripMenuItem.Checked = Settings.Default.ifuseftp; toolStripMenuItem1.Text = Settings.Default.Printnum.ToString(); //string req = File.ReadAllText(@"C:\Users\Hcsy\Documents\Tencent Files\814802334\FileRecv\4.txt", Encoding.UTF8); //if (req.Contains("\"" + "chooseprintername" + "\"")) //{ // req = req.Remove(req.IndexOf("device"), req.IndexOf("data") - 2); // lock ("A") // { // PrintHandler.vendorZplPrint(req); // } //} //else //{ // PrintHandler.zplprint(req); //} } 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; StreamReader sr = new StreamReader(stream); string req = sr.ReadToEnd(); sr.Close(); 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 PrintC = new List(); 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; } private void FTPtoolStripMenuItem_Click(object sender, EventArgs e) { Settings.Default.ifuseftp = FTPtoolStripMenuItem.Checked; Settings.Default.Save(); } private void toolStripMenuItem1_TextChanged(object sender, EventArgs e) { try { int a = int.Parse(toolStripMenuItem1.Text); Settings.Default.Printnum = a; Settings.Default.Save(); } catch { MessageBox.Show("录入数量须为整数"); Settings.Default.Printnum = 1; Settings.Default.Save(); toolStripMenuItem1.Text = "1"; } } } }