using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; using Lskj.Control.Model; using Lskj.Model; using Lskj.Business.Impl; using Lskj.Util; using System.Web; using Lskj.Business; using Lskj.Control; using Xilium.CefGlue; using Lskj.Control.BrowserSetting2; using System.Net; using System.Collections; using Lskj.Web.Core.Util; using System.Linq; namespace Lskj.Control { public partial class FrmWebBrowser2 : UserControl { public DynamicPubBrowerModel PubBrowerModel; public CefBrowser cefBrowserSettings; /// /// 是否允许下载 /// public bool AllowDownload = true; public FrmWebBrowser2() { InitializeComponent(); } void Form1_FormClosed(object sender, FormClosedEventArgs e) { //CefRuntime.Shutdown(); } public void FrmLoad(string url, int RightKeyFlag = 0, bool enUrl = true) { var cefWindowInfo = CefWindowInfo.Create(); cefWindowInfo.SetAsChild(this.Handle, new CefRectangle(0, 0, this.Width, this.Height)); var bsClient = new BsClient(RightKeyFlag, this.AllowDownload); bsClient.frmWebBrowser = this; bsClient.OnCreated += bsClient_OnCreated; var cefBrowserSettings = new CefBrowserSettings() { }; if (enUrl) { url = ToEnUrl(url); } //Url = "https://ie.icoa.cn/"; CefBrowserHost.CreateBrowser(cefWindowInfo, bsClient, cefBrowserSettings, url); //清除所有Cookie var cookieManager = CefCookieManager.GetGlobal(null); cookieManager.DeleteCookies("", "", null); } private void bsClient_OnCreated(object sender, EventArgs e) { cefBrowserSettings = (CefBrowser)sender; SyncBrowserBounds(); } protected override void OnResize(EventArgs e) { base.OnResize(e); SyncBrowserBounds(); } /// /// 将CEF原生子窗口同步到当前控件的客户区,修正高DPI或父容器布局变化造成的位置偏移。 /// public void SyncBrowserBounds() { if (cefBrowserSettings == null || IsDisposed || Disposing || ClientSize.Width <= 0 || ClientSize.Height <= 0) { return; } ResizeWindow(cefBrowserSettings.GetHost().GetWindowHandle(), ClientSize.Width, ClientSize.Height); } public void ResizeWindow(IntPtr handle, int width, int height) { if (handle != IntPtr.Zero) { const uint SWP_NOZORDER = 0x0004; const uint SWP_NOACTIVATE = 0x0010; NativeMethod.SetWindowPos(handle, IntPtr.Zero, 0, 0, width, height, SWP_NOZORDER | SWP_NOACTIVATE ); } } public void LoadUrl(string url, bool enUrl = true) { if (cefBrowserSettings != null) { if (enUrl) { url = ToEnUrl(url); } cefBrowserSettings.GetMainFrame().LoadUrl(url); } else { if (enUrl) { url = ToEnUrl(url); } FrmLoad(url); } } public void LoadRequest(string url, string dataStr) { BrowserSetting2.HttpTools.setting("application/x-www-form-urlencoded", null, null); Dictionary dataDic = new Dictionary(); dataDic.Add("billCode", dataStr); HttpWebResponse webResponse = BrowserSetting2.HttpTools.Post(url, "", dataDic, null, HttpTools.Method.POST); if (webResponse != null) { cefBrowserSettings.GetMainFrame().LoadUrl(webResponse.ResponseUri.AbsoluteUri); } } public static string ToEnUrl(string url) { string[] urlParams = url.Split('?'); string enKey = "encrypt", enVal = ""; bool hasEn = false; if (urlParams.Length > 1) { string[] queryParams = string.Join("?", urlParams.Skip(1)).Split('&'); List qpLS = new List(); Hashtable pms = new Hashtable(); hasEn = queryParams.Any(str => str.Trim().StartsWith(enKey, StringComparison.OrdinalIgnoreCase)); foreach (string q in queryParams) { string[] eqParams = q.Split('='); if (eqParams.Length > 1) { if (hasEn && eqParams[0].ToLower() == enKey) { enVal = string.Join("=", eqParams.Skip(1)); continue; } if (hasEn || eqParams[0].ToLower() == "username" || eqParams[0].ToLower() == "password") { pms.Add(eqParams[0], string.Join("=", eqParams.Skip(1))); } else { string rawValue = HttpUtility.UrlDecode(string.Join("=", eqParams.Skip(1))).TrimEnd(); qpLS.Add(string.Format("{0}={1}", eqParams[0], HttpUtility.UrlEncode(rawValue).Replace("%5c", "/").Replace("%2f", "/").Replace("//", "/").Replace("%3d", "=").Replace("+", "%20"))); } } else { } } if (pms.Count > 0) { DateTime dateTime = DateTime.Now.AddDays(1); DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); long timestamp = (dateTime.Ticks - epoch.Ticks) / TimeSpan.TicksPerSecond; pms.Add("exp", timestamp); } var allowToEnUrl = qpLS.Where(n => n.Equals("ToEnUrl=0", StringComparison.OrdinalIgnoreCase)).FirstOrDefault() != null; if (allowToEnUrl) { return url; } else { if (url.Contains("pms=")) return $"{urlParams[0]}?{HttpUtility.UrlEncode(hasEn ? Lskj.Web.Core.Util.safety.AESUtil.Encrypt(JSON.Encode(pms), enVal) : Lskj.Web.Core.Util.safety.AESUtil.MobileEncrypt(JSON.Encode(pms)))}&{qpLS.SJoin("&")}"; return $"{urlParams[0]}?pms={HttpUtility.UrlEncode(hasEn ? Lskj.Web.Core.Util.safety.AESUtil.Encrypt(JSON.Encode(pms), enVal) : Lskj.Web.Core.Util.safety.AESUtil.MobileEncrypt(JSON.Encode(pms)))}&{qpLS.SJoin("&")}"; } } return url; } } }