using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using Lskj.Util; using Gecko; using Gecko.Events; using Lskj.Business.Impl; using System.Runtime.InteropServices; using System.Web; using System.Net; using System.Text.RegularExpressions; namespace Lskj.Control { public partial class ModuleWebView : UserControl { public GeckoWebBrowser browser; public WebBrowser browserNew; public static bool Evenflag; public string Url; public bool RightKeyflag; private string _saveDir; public EventHandler OnNavigating; public ModuleWebView() { InitializeComponent(); } /// /// 说明:打开网页 /// 创建人:龚宇超 /// 创建日期:2019-03-14 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// public void Navigate(string url, int RightKeyFlag = 1) { this.RightKeyflag = RightKeyFlag == 1; Navigate(url, GeckoLoadFlags.None); } /// /// 说明: /// 创建人:龚宇超 /// 创建日期:2019-12-24 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The URL. /// The flag. public void Navigate(string url, GeckoLoadFlags flag) { try { if (Directory.Exists(PubUtil.WebViewPath)) { this.Url = url; string directory = Path.Combine(new string[] { PubUtil.AbsolutelyPath + "cookie" }); //if (!Directory.Exists(directory + "\\cookies.dat")) // Directory.CreateDirectory(directory + "\\cookies.dat");//检测目录是否存在 //string fileText = directory + "\\cookies.dat"; //Gecko.Xpcom.ProfileDirectory = directory + "\\cookies.dat";//绑定cookie目录 //Gecko.Xpcom.ProfileDirectory = GetCookiesFromFile;//绑定cookie目录 //url = url + "&t=" + DateTime.Now.Ticks; if (browser != null) { browser.Dispose(); browser = null; } Xpcom.Initialize(PubUtil.WebViewPath); browser = new GeckoWebBrowser(); browser.Dock = DockStyle.Fill; browser.Name = "browser"; browser.NoDefaultContextMenu = !RightKeyflag; //禁用右键菜单 if (OnNavigating != null) { browser.Navigating += OnNavigating; } this.panelControl1.Controls.Add(browser); if (!Evenflag) { Gecko.LauncherDialog.Download += new EventHandler(OnLauncherDialogDownload); Evenflag = true; } browser.Navigate(url, flag); IniHelper.Write(url, browser.Document.Cookie); } else { MessageUtil.Show("未找到浏览器资源文件,请联系开发人员."); } } catch (Exception ex) { MessageUtil.Show("浏览器出错!" + ex); } } /// /// 说明:解析 cookies.dat文件得到Cookie,没有判断 path,只有 域的判断 /// 创建人:王一帆 /// 创建日期:2021-06-28 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The URL. /// The flag. public string GetCookiesFromFile { get { StringBuilder sbCookie = new StringBuilder(); string filePath = Path.Combine(new string[] { PubUtil.AbsolutelyPath + "cookie" }); if (File.Exists(filePath + "\\cookies.dat")) { var uri = new Uri(Url); var host = uri.Host; var allCookies = File.ReadAllLines(filePath + "\\cookies.dat"); for (int i = 4; i < allCookies.Length; i++) { host = uri.Host; var listCookie = allCookies[i].Split('\t'); if (listCookie != null && listCookie.Length != 0 && listCookie.Length == 7) { var _cookie = listCookie[0]; Lable: if (_cookie == host) { sbCookie.AppendFormat("{0}={1};", listCookie[5], listCookie[6]); } //httponly var httpOnly = "#HttpOnly_" + host; if (_cookie == httpOnly) { sbCookie.AppendFormat("{0}={1};", listCookie[5], listCookie[6]); } if (host.IndexOf('.') == 0)// . 开头 { host = host.Substring(host.IndexOf('.') + 1);//. 开头 去掉 . goto Lable; } else { if (host.TrimStart('.').Split('.').Length > 2) { host = host.Substring(host.IndexOf('.'));//带 . goto Lable; } } } } } return sbCookie.ToString(); } } /// /// 触发下载事件 /// /// /// void OnLauncherDialogDownload(Object sender, LauncherDialogEvent e) { try { string tipMsg = string.Empty; int vsNum = 0; DataRow rowItem = AttachImpl.GetTempletFile(e.Filename); string topname = e.Filename.Substring(0, e.Filename.LastIndexOf(".") - 1); bool isMultipleLoad = e.Url.Contains("downLoadTemp") && Regex.IsMatch(topname, @"^[+-]?\d*[.]?\d*$"); if (rowItem != null || isMultipleLoad) { try { bool success = rowItem != null ? AttachImpl.CheckAttachOperation(2, "", Convert.ToInt32(rowItem["fileId"]), "", 0, out tipMsg) == 1 : isMultipleLoad; if (success) { if (browserNew != null) { browserNew.Dispose(); browserNew = null; } browserNew = new WebBrowser();//点击下载的页面 this.panelControl1.Controls.Add(browserNew); //downfile(e.Url, e.Filename, tempPath); Download(e.Url, e.Filename); //browserNew.Navigate(e.Url); } else { MessageUtil.Show(tipMsg); } } catch (Exception ex) { MessageUtil.Show("下载失败!\r\n" + ex.Message); } } else { MessageUtil.Show("未找到下载项"); } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } } /// /// 说明:定义服务器文件地址,含文件名。客户端接收地址及文件名由客户端自行定义 /// 创建人:王一帆 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The URL. /// Name of the file. public static void Download(string URL, string FileName) { FrmSelectDownload frmSelectDownload = new FrmSelectDownload(); frmSelectDownload.URL = URL; frmSelectDownload.FileName = FileName; frmSelectDownload.Show(); //SaveFileDialog saveFileDialog = new SaveFileDialog(); //saveFileDialog.FileName = FileName; //saveFileDialog.Title = "下载模板文件到"; //saveFileDialog.RestoreDirectory = true; ////点了保存按钮进入 //if (saveFileDialog.ShowDialog() == DialogResult.OK) //{ // string localFilePath = System.IO.Path.GetFullPath(saveFileDialog.FileName);//获得文件路径,含文件名 // string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//获取文件名,不带路径 // string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));//获取文件路径,不带文件名 // FrmDownload frm = new FrmDownload(URL, FileName, localFilePath); // frm.Show(); //} } public void FDispose() { if (browser != null) { browser.Dispose(); } this.Dispose(); } } }