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) { httpPostRequest.Prefixes.Add("http://localhost:9100/"); httpPostRequest.Start(); ThrednHttpPostRequest = new Thread(new ThreadStart(httpPostRequestHandle)); ThrednHttpPostRequest.Start(); MyNotifyIcon.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; //获取Post请求中的参数和值帮助类 HttpListenerPostParaHelper httppost = new HttpListenerPostParaHelper(request); //获取Post过来的参数和数据 List lst = httppost.GetHttpListenerPostValue(); string userName = ""; string password = ""; string suffix = ""; string adType = ""; //使用方法 //foreach (var key in lst) //{ // if (key.type == 0) // { // string value = Encoding.UTF8.GetString(key.datas).Replace("\r\n", ""); // if (key.name == "username") // { // userName = value; // Console.WriteLine(value); // } // if (key.name == "password") // { // password = value; // Console.WriteLine(value); // } // if (key.name == "suffix") // { // suffix = value; // Console.WriteLine(value); // } // if (key.name == "adtype") // { // adType = value; // Console.WriteLine(value); // } // } // if (key.type == 1) // { // string fileName = request.Request.QueryString["FileName"]; // if (!string.IsNullOrEmpty(fileName)) // { // string filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyMMdd_HHmmss_ffff") + Path.GetExtension(fileName).ToLower(); // if (key.name == "File") // { // FileStream fs = new FileStream(filePath, FileMode.Create); // fs.Write(key.datas, 0, key.datas.Length); // fs.Close(); // fs.Dispose(); // } // } // } //} //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 = 10 * 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); 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) { ThrednHttpPostRequest.Abort(); httpPostRequest.Close(); this.Close(); this.Dispose(); } } }