using CefSharp; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text.RegularExpressions; using System.Windows.Forms; using System.Drawing.Printing; using FastReport; using System.Data; using System.Threading; using System.Net; using System.Text; using System.Web; using System.Diagnostics; using UAS_Web.Properties; namespace UAS_Web.tool { class RequestHandler : IRequestHandler { public event Action NotifyMsg; //打印图片集合 List bitmaps = new List(); Report BarcodeReport = new Report(); Dictionary itemlast; Dictionary itemBox; //图片打印中间变量 int imagecount = 0; int index = 0; string PrinterName = ""; string PrintType = ""; PrinterList print; string LABELNAME = ""; string labelview = ""; Dictionary DataCollection = null; //打印全部标签所需变量 string AllLabelname = ""; string AllBoxname = ""; //打印类型,箱号条码统一打印或者是单一打印 string PType = ""; /// /// 拦截指定请求 /// /// /// /// /// /// /// IResponseFilter IRequestHandler.GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response) { MessageFilter filter; switch (request.Url.Substring(request.Url.LastIndexOf("/") + 1)) { case "zplPrinter": filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter; return filter; case "zplPrint.action": filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter; return filter; case "getPrintType.action": filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter; return filter; case "vendorZplPrint.action": filter = FilterManager.CreateFilter(request.Identifier.ToString()) as MessageFilter; return filter; default: break; } return null; } /// /// 完成响应后获取Js返回的数据 /// /// /// /// /// /// /// /// void IRequestHandler.OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength) { MessageFilter filter; string str; Dictionary data; //打印条码时总页数与当前页变量 switch (request.Url.Substring(request.Url.LastIndexOf("/") + 1)) { case "zplPrinter": var m = request.Method; if (request.Method == "POST") { using (var postData = request.PostData) { if (postData != null) { var elements = postData.Elements; var charSet = request.GetCharSet(); foreach (var element in elements) { if (element.Type == PostDataElementType.Bytes) { string body = HttpUtility.UrlDecode(element.GetBody(charSet),Encoding.UTF8); LABELNAME = body.Substring(body.LastIndexOf('=')+1); } } } } } PType = "normal"; //弹出打印机选择列表 print = new PrinterList(browser) { StartPosition = FormStartPosition.CenterScreen,TopMost=true }; print.Controls["Confirm"].Click += RequestHandler_Click; print.PrintDoc.PrintPage += PrintPage; print.ShowDialog(); break; case "zplPrinterAll": if (request.Method == "POST") { using (var postData = request.PostData) { if (postData != null) { var elements = postData.Elements; var charSet = request.GetCharSet(); foreach (var element in elements) { if (element.Type == PostDataElementType.Bytes) { string body = HttpUtility.UrlDecode(element.GetBody(charSet), Encoding.UTF8); //LABELNAME = body.Substring(body.LastIndexOf('=') + 1); AllLabelname = body.Substring(body.IndexOf("la_name=") + 8, body.IndexOf("&la_idBox")- (body.IndexOf("la_name=") + 8)); AllBoxname = body.Substring(body.LastIndexOf('=') + 1); } } } } } PType = "all"; //弹出打印机选择列表 print = new PrinterList(browser) { StartPosition = FormStartPosition.CenterScreen, TopMost = true }; print.Controls["Confirm"].Click += RequestHandler_Click; print.PrintDoc.PrintPage += PrintPage; print.ShowDialog(); break; case "zplPrint.action": filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter; str = System.Text.Encoding.UTF8.GetString(filter.dataAll.ToArray()); data = ToDictionary(str); //获取所有的打印格式数据 if (data.ContainsKey("data")) { string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", ""); List PrintList = new List(); int PrintTime = Regex.Matches(PrintCode, "XA").Count; for (int i = 0; i < PrintTime; i++) { PrintList.Add(PrintCode.Substring(0, PrintCode.IndexOf("XZ") + 2)); PrintCode = PrintCode.Substring(PrintCode.IndexOf("XZ") + 2); } for (int i = 0; i < PrintList.Count; i++) { PrintHelper.SendStringToPrinter(PrinterName, PrintList[i]); } data.Clear(); } else if (data.ContainsKey("exceptionInfo")) { //string PrintCode = data["exceptionInfo"].ToString(); //MessageBox.Show(PrintCode); } break; case "getPrintType.action": filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter; str = System.Text.Encoding.UTF8.GetString(filter.dataAll.ToArray()); data = ToDictionary(str); PrintType = data["data"].ToString().Replace("\"", ""); break; case "vendorZplPrint.action": try { filter = FilterManager.GetFileter(request.Identifier.ToString()) as MessageFilter; str = System.Text.Encoding.UTF8.GetString(filter.dataAll.ToArray()); if (str.Contains("不允许打印")||(str.Contains("exceptionInfo"))) return; data = ToDictionary(str); if (DataCollection == null) { DataCollection = new Dictionary(); DataCollection.Add(((string)data["currentPage"]).Replace("\"", ""), data); } if (DataCollection.Count < int.Parse(((string)data["totalPage"]).Replace("\"", ""))) { if(!DataCollection.ContainsKey(((string)data["currentPage"]).Replace("\"", ""))) DataCollection.Add(((string)data["currentPage"]).Replace("\"", ""), data); } if (DataCollection.Count == int.Parse(((string)data["totalPage"]).Replace("\"", ""))) { if (PType == "normal") { for (int i = 0; i < DataCollection.Count; i++) { vendorZplPrint(DataCollection[(i + 1).ToString()]); } } else { Console.WriteLine("打印ALL"); for (int i = 0; i < DataCollection.Count; i++) { vendorZplPrintAll(DataCollection[(i + 1).ToString()]); } } DataCollection = null; } //vendorZplPrint(data); //vendorZplPrintAll(data); } catch (Exception ex) { MessageBox.Show(ex.Message + "请重新进行打印"); DataCollection = null; return; } break; default: break; } } public void vendorZplPrint(Object data) { DataTable dt = new DataTable(); RadioButton PRFR = (RadioButton)print.Controls["PRFR"]; RadioButton PRPIC = (RadioButton)print.Controls["PRPIC"]; string type; bitmaps.Clear(); List> parameter = new List>(); List> barcode = new List>(); try { parameter = (List>)((Dictionary)data)["parameter"]; barcode = (List>)((Dictionary)data)["barcode"]; } catch { MessageBox.Show("打印数据异常,请联系管理员"); return; } try { type = ((String)((Dictionary)data)["type"]).Replace("\"", ""); } catch { type = "no"; } try { labelview = ((String)((Dictionary)data)["label_type"]).Replace("\"", ""); } catch { labelview = ""; } if (PRFR.Checked) { Thread.Sleep(1000); int i = 0; while (DownLoadFile.downloading && i < 2) { Thread.Sleep(1000); i++; } if (DownLoadFile.downloading) MessageBox.Show("标签更新有误"); try { BarcodeReport.Load(System.AppDomain.CurrentDomain.BaseDirectory + LABELNAME + ".frx"); } catch { MessageBox.Show("未找到可使用的标签文件,请重试"); return; } } int la_width = 1; int la_height = 1; foreach (var item in barcode) { Bitmap bit = new Bitmap(la_width * 10, la_height * 4); using (Graphics g = Graphics.FromImage(bit)) { foreach (var itemc in item) { Dictionary Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper()); if (Sidc != null) { if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text") { string itemstr = ""; if (itemc.Value != null) { itemstr = itemc.Value.ToString(); while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n")) { itemstr = itemstr.Remove(0, 2); } if (itemstr.StartsWith("\"")) { itemstr = itemstr.Remove(0, 1); } while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n")) { itemstr = itemstr.Remove(itemstr.Length - 2); } if (itemstr.EndsWith("\"")) { itemstr = itemstr.Remove(itemstr.Length - 1); } if (itemstr.Contains("\\\"")) { itemstr = itemstr.Replace("\\\"", "\""); } } if (PRFR.Checked) { DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String")); if (!dt.Columns.Contains(itemc.Key)) dt.Columns.Add(DC); } if (PRPIC.Checked) { PrintHelper.Printstring(itemc.Value != null ? itemstr : " ", int.Parse(Sidc["LP_SIZE"].ToString()), g, int.Parse(Sidc["LP_LEFTRATE"].ToString()), int.Parse(Sidc["LP_TOPRATE"].ToString())); } } else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode") { if (PRFR.Checked) { DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String")); if (!dt.Columns.Contains(itemc.Key)) dt.Columns.Add(DC); } if (PRPIC.Checked) { PrintHelper.DrawBarcode(itemc.Value != null ? itemc.Value.ToString().Replace("\"", "") : " ", ref bit, double.Parse(Sidc["LP_LEFTRATE"].ToString()), double.Parse(Sidc["LP_TOPRATE"].ToString()), double.Parse(Sidc["LP_WIDTH"].ToString()), double.Parse(Sidc["LP_HEIGHT"].ToString()) - 2); PrintHelper.Printstring(itemc.Value != null ? itemc.Value.ToString().Replace("\"", "") : " ", int.Parse(Sidc["LP_SIZE"].ToString()), g, int.Parse(Sidc["LP_LEFTRATE"].ToString()), int.Parse(Sidc["LP_TOPRATE"].ToString()) + double.Parse(Sidc["LP_HEIGHT"].ToString()) - 2); } } } } } bitmaps.Add(bit); } if (PRFR.Checked) { foreach (var item in barcode) { for (int i = 0; i < Settings.Default.Printnum; i++) { DataRow dr = dt.NewRow(); //g.CompositingQuality = CompositingQuality.HighQuality; //g.SmoothingMode = SmoothingMode.HighQuality; //g.InterpolationMode = InterpolationMode.HighQualityBicubic; ////定义一枝画笔 //Pen pen = new Pen(Color.Black); ////用这个画笔在左上角画一个矩形 //g.DrawRectangle(pen, new Rectangle(10, 10, 50, 20)); foreach (var itemc in item) { Dictionary Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper()); if (Sidc != null) { if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text") { string itemstr = ""; if (itemc.Value != null) { itemstr = itemc.Value.ToString(); while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n")) { itemstr = itemstr.Remove(0, 2); } if (itemstr.StartsWith("\"")) { itemstr = itemstr.Remove(0, 1); } while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n")) { itemstr = itemstr.Remove(itemstr.Length - 2); } if (itemstr.EndsWith("\"")) { itemstr = itemstr.Remove(itemstr.Length - 1); } if (itemstr.Contains("\\\"")) { itemstr = itemstr.Replace("\\\"", "\""); } } dr[itemc.Key] = itemstr; } else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode") { dr[itemc.Key] = itemc.Value.ToString().Replace("\"", ""); } } } dt.Rows.Add(dr); } } } if (PRPIC.Checked) { if (bitmaps.Count > 0) { imagecount = bitmaps.Count; print.PrintDoc.Print(); } } if (PRFR.Checked) { try { if (labelview == "") { if (type == "box") { BarcodeReport.RegisterData(dt, "VENDORBARCODE_BOX_VIEW"); BarcodeReport.GetDataSource("VENDORBARCODE_BOX_VIEW").Enabled = true; BarcodeReport.PrintSettings.ShowDialog = false; BarcodeReport.PrintSettings.Printer = PrinterName; BarcodeReport.Print(); } else { BarcodeReport.RegisterData(dt, "VENDORBARCODE_VIEW"); BarcodeReport.GetDataSource("VENDORBARCODE_VIEW").Enabled = true; BarcodeReport.PrintSettings.ShowDialog = false; BarcodeReport.PrintSettings.Printer = PrinterName; BarcodeReport.Print(); } } else { BarcodeReport.RegisterData(dt, labelview); BarcodeReport.GetDataSource(labelview).Enabled = true; BarcodeReport.PrintSettings.ShowDialog = false; BarcodeReport.PrintSettings.Printer = PrinterName; BarcodeReport.Print(); } } catch(Exception ex) { MessageBox.Show(ex.Message+"打印出现错误,请检查标签文件选择是否正确"); } dt.Clear(); } } string nowboxcode = ""; public void vendorZplPrintAll(Object data) { //条码标签结构datatable DataTable dt = new DataTable(); //箱号标签结构datatable DataTable box = new DataTable(); //记录最新打印出来的箱号 string type; //箱号对应视图名 string box_type; bitmaps.Clear(); List> parameter = new List>(); List> BoxParameter = new List>(); //List> parameter2 = new List>(); List> barcode = new List>(); List> BoxBarcode = new List>(); try { parameter = (List>)((Dictionary)data)["parameter"]; BoxParameter = (List>)((Dictionary)data)["BoxParameter"]; barcode = (List>)((Dictionary)data)["barcode"]; BoxBarcode = (List>)((Dictionary)data)["BoxBarcode"]; } catch(Exception ex) { MessageBox.Show(ex.Message+"打印数据异常,请联系管理员"); return; } try { type = ((String)((Dictionary)data)["type"]).Replace("\"", ""); } catch { type = "no"; } try { labelview = ((String)((Dictionary)data)["label_type"]).Replace("\"", ""); } catch { labelview = ""; } try { box_type = ((String)((Dictionary)data)["BoxLabel_type"]).Replace("\"", ""); } catch { box_type = ""; } Thread.Sleep(1000); int k = 0; while (DownLoadFile.downloading && k < 2) { Thread.Sleep(1000); k++; } if (DownLoadFile.downloading) MessageBox.Show("标签更新有误"); //barcode条码datable格式 foreach (var item in parameter) { foreach (var itemc in item) { string itemstr = ""; if (itemc.Value != null && itemc.Key.ToString().ToUpper() == "LP_NAME") { itemstr = itemc.Value.ToString(); while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n")) { itemstr = itemstr.Remove(0, 2); } if (itemstr.StartsWith("\"")) { itemstr = itemstr.Remove(0, 1); } while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n")) { itemstr = itemstr.Remove(itemstr.Length - 2); } if (itemstr.EndsWith("\"")) { itemstr = itemstr.Remove(itemstr.Length - 1); } if (itemstr.Contains("\\\"")) { itemstr = itemstr.Replace("\\\"", "\""); } DataColumn DC = new DataColumn(itemstr, Type.GetType("System.String")); if (!dt.Columns.Contains(itemc.Key)) dt.Columns.Add(DC); } } } //box箱号datatable格式 foreach (var item in BoxParameter) { foreach (var itemc in item) { string itemstr = ""; if (itemc.Value != null && itemc.Key.ToString().ToUpper() == "LP_NAME") { itemstr = itemc.Value.ToString(); while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n")) { itemstr = itemstr.Remove(0, 2); } if (itemstr.StartsWith("\"")) { itemstr = itemstr.Remove(0, 1); } while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n")) { itemstr = itemstr.Remove(itemstr.Length - 2); } if (itemstr.EndsWith("\"")) { itemstr = itemstr.Remove(itemstr.Length - 1); } if (itemstr.Contains("\\\"")) { itemstr = itemstr.Replace("\\\"", "\""); } DataColumn DC = new DataColumn(itemstr, Type.GetType("System.String")); if (!box.Columns.Contains(itemc.Key)) box.Columns.Add(DC); } } } for (int i = 0; i < barcode.Count; i++) { Dictionary item = barcode[i]; if (i == 0 && item["BAN_OUTBOXCODE"].ToString() != nowboxcode && nowboxcode != "") { try { itemBox = BoxBarcode.Find(p => p["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", "") == itemlast["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", "")); FillPrintData(box, itemBox, BoxParameter); PrintLabel(box, box_type, AllBoxname); Console.WriteLine("一箱" + itemlast["BAN_OUTBOXCODE"].ToString()); } catch { nowboxcode = ""; } } if (i > 0 && item["BAN_OUTBOXCODE"].ToString() != barcode[i - 1]["BAN_OUTBOXCODE"].ToString()) { PrintLabel(dt,labelview, AllLabelname); itemlast = barcode[i - 1]; try { itemBox = BoxBarcode.Find(p => p["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", "") == itemlast["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", "")); FillPrintData(box, itemBox, BoxParameter); PrintLabel(box, box_type, AllBoxname); Console.WriteLine("一箱" + itemlast["BAN_OUTBOXCODE"].ToString()); } catch {} } FillPrintData(dt, item, parameter); } PrintLabel(dt, labelview,AllLabelname); itemlast = barcode[barcode.Count-1]; if (((String)((Dictionary)data)["currentPage"]).Replace("\"", "") == ((String)((Dictionary)data)["totalPage"]).Replace("\"", "")) { try { itemBox = BoxBarcode.Find(p => p["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", "") == itemlast["BAN_OUTBOXCODE"].ToString().Replace(" ", "").Replace("\"", "")); FillPrintData(box, itemBox, BoxParameter); PrintLabel(box, box_type, AllBoxname); Console.WriteLine("一箱" + itemBox["BAN_OUTBOXCODE"].ToString()); } catch { } nowboxcode = ""; } else { nowboxcode = itemlast["BAN_OUTBOXCODE"].ToString(); } dt.Clear(); } //给datable添加行(添加打印具体数据) private void FillPrintData(DataTable dt, Dictionary item, List> parameter1) { DataRow dr = dt.NewRow(); foreach (var itemc in item) { Dictionary Sidc = parameter1.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper()); if (Sidc != null) { if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text") { string itemstr = ""; if (itemc.Value != null) { itemstr = itemc.Value.ToString(); while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n")) { itemstr = itemstr.Remove(0, 2); } if (itemstr.StartsWith("\"")) { itemstr = itemstr.Remove(0, 1); } while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n")) { itemstr = itemstr.Remove(itemstr.Length - 2); } if (itemstr.EndsWith("\"")) { itemstr = itemstr.Remove(itemstr.Length - 1); } if (itemstr.Contains("\\\"")) { itemstr = itemstr.Replace("\\\"", "\""); } } dr[itemc.Key] = itemstr; } else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode") { dr[itemc.Key] = itemc.Value.ToString().Replace("\"", ""); } } } dt.Rows.Add(dr); } private void PrintLabel(DataTable dt,string labelview,string LABELNAME) { try { BarcodeReport.Load(System.AppDomain.CurrentDomain.BaseDirectory + LABELNAME + ".frx"); } catch { MessageBox.Show("未找到可使用的标签文件,请重试"); return; } try { BarcodeReport.RegisterData(dt, labelview); BarcodeReport.GetDataSource(labelview).Enabled = true; BarcodeReport.PrintSettings.ShowDialog = false; BarcodeReport.PrintSettings.Printer = PrinterName; BarcodeReport.Print(); CleanDataTable(dt); } catch (Exception ex) { MessageBox.Show(ex.Message + "打印出现错误,请检查标签文件选择是否正确"); } } private void PrintPage(object sender, PrintPageEventArgs e) { Image i = bitmaps[index]; Graphics g = e.Graphics; g.DrawImage(i, 4, 10, i.Width, i.Height); if (index < imagecount - 1) { e.HasMorePages = true; index++; } else { index = 0; e.HasMorePages = false; } } private void RequestHandler_Click(object sender, EventArgs e) { //设置打印机名称 print.PrintDoc.PrinterSettings.PrinterName = print.Controls["Printer"].Text; PrinterName = print.Controls["Printer"].Text; RadioButton PRXA = (RadioButton)print.Controls["PRXA"]; RadioButton PRPIC = (RadioButton)print.Controls["PRPIC"]; int a = int.Parse(print.Controls["comboBox1"].Text); Settings.Default.Printnum = a; Settings.Default.Save(); //获取矩阵图的分辨率 Graphics gr = print.PrintDoc.PrinterSettings.CreateMeasurementGraphics(); print.browser.FocusedFrame.ExecuteJavaScriptAsync("(function(value,value1){dpi=value,printType=value1})('" + gr.DpiX + "','" + PrintType + "')"); if (PType == "all") { print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmBarcodeAndBoxPrint').click();"); } else { if (PRXA.Checked) { print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmZplPrint').click();"); } else { print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmPicturePrint').click();"); } } print.Close(); } void filter_NotifyData(byte[] data) { if (NotifyMsg != null) { NotifyMsg(data); } } public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback) { return false; } public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect) { return false; } public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback) { return CefReturnValue.Continue; } public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback) { return false; } public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture) { return false; } public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath) { } public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url) { return false; } public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback) { return false; } public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status) { } public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser) { } public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength) { } public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl) { } bool IRequestHandler.OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response) { return false; } public void GetData() { } public static void CleanDataTable(DataTable dt) { for (int i = dt.Rows.Count - 1; i >= 0; i--) dt.Rows.Remove(dt.Rows[i]); } public static Dictionary ToDictionary(string JsonData) { object Data = null; Dictionary Dic = new Dictionary(); if (JsonData.StartsWith("[")) { //如果目标直接就为数组类型,则将会直接输出一个Key为List的List>集合 //使用示例List> ListDic = (List>)Dic["List"]; List> List = new List>(); MatchCollection ListMatch = Regex.Matches(JsonData, @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组 foreach (Match ListItem in ListMatch) { List.Add(ToDictionary(ListItem.ToString()));//递归调用 } Data = List; Dic.Add("List", Data); } else { MatchCollection Match = Regex.Matches(JsonData, @"""(.+?)"": {0,1}(\[[\s\S]+?\}\s*\]|null|"".+?""(,+?)|"".+?""(\s*?)(\}+?)|-{0,1}\d*)");//使用正则表达式匹配出JSON数据中的键与值 foreach (Match item in Match) { try { if (item.Groups[2].ToString().StartsWith("[")) { //如果目标是数组,将会输出一个Key为当前Json的List>集合 //使用示例List> ListDic = (List>)Dic["Json中的Key"]; List> List = new List>(); MatchCollection ListMatch = Regex.Matches(item.Groups[2].ToString(), @"{[\s\S]+?}");//使用正则表达式匹配出JSON数组 foreach (Match ListItem in ListMatch) { List.Add(ToDictionary(ListItem.ToString()));//递归调用 } Data = List; } else if (item.Groups[2].ToString().ToLower() == "null") Data = null;//如果数据为null(字符串类型),直接转换成null else if (item.Groups[2].ToString().EndsWith(",")) Data = item.Groups[2].ToString().Remove(item.Groups[2].ToString().Length - 1); //数据为数字、字符串中的一类,则去掉,直接写入 else if (item.Groups[2].ToString().EndsWith("}")) Data = item.Groups[2].ToString().Remove(item.Groups[2].ToString().Length - 1); //数据为数字、字符串中的一类,则去掉,直接写入 else Data = item.Groups[2].ToString(); Dic.Add(item.Groups[1].ToString(), Data); } catch { } } } return Dic; } } }