public void Navigate(Uri requestUrl, HttpRequestContent content = null) { var arg = new ScriptingContext(requestUrl, content); Navigate(arg); arg.WaitHandle.WaitOne(); }
private void Navigate(ScriptingContext arg) { MLA.Invoke(() => { if (browser == null) { browser = new NonDialogWebBrowser(); #if !DEBUG browser.Hide(); #endif ClientForm.xInit(browser); browser.DocumentCompleted += browser_DocumentCompleted; } browser.ObjectForScripting = arg; byte[] postData = null; string headers = null; if (arg.RequestContent != null) { if (this.UseCookies) { if (arg.RequestContent.HasCookie) { _cookieContainer.Add(arg.RequestUrl, arg.RequestContent.Cookies); } string cookieHeader = arg.RequestContent.Headers[HttpRequestHeader.Cookie]; if (!string.IsNullOrEmpty(cookieHeader)) { _cookieContainer.SetCookies(arg.RequestUrl, cookieHeader.Replace(';', ',')); arg.RequestContent.Headers.Remove(HttpRequestHeader.Cookie); } cookieHeader = _cookieContainer.GetCookieHeader(arg.RequestUrl); if (cookieHeader.Length > 0) { arg.RequestContent.Headers[HttpRequestHeader.Cookie] = cookieHeader.Replace(',', ';'); } //WinInetInterop.SaveCookies(_cookieContainer, absoluteUri); } else { arg.RequestContent.Headers[HttpRequestHeader.Cookie] = string.Empty; //WinInetInterop.DeleteCache(WinInetInterop.CacheKind.Cookies); } if (arg.RequestContent.HasBody) { arg.RequestContent.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; postData = Encoding.UTF8.GetBytes(arg.RequestContent.GetFormString()); } headers = arg.RequestContent.GetHeadersString(); } browser.Navigate(arg.RequestUrl, "_self", postData, headers); }); this.STA_WaitSr(); }
public void Navigate(Uri requestUrl, HttpRequestContent content = null) { var arg = new ScriptingContext(requestUrl, content); var browser = webBrowser1; browser.ObjectForScripting = arg; byte[] postData = null; string headers = null; if (arg.RequestContent != null) { if (this.UseCookies) { if (arg.RequestContent.HasCookie) { _cookieContainer.Add(arg.RequestUrl, arg.RequestContent.Cookies); } string cookieHeader = arg.RequestContent.Headers[HttpRequestHeader.Cookie]; if (!string.IsNullOrEmpty(cookieHeader)) { _cookieContainer.SetCookies(arg.RequestUrl, cookieHeader.Replace(';', ',')); arg.RequestContent.Headers.Remove(HttpRequestHeader.Cookie); } cookieHeader = _cookieContainer.GetCookieHeader(arg.RequestUrl); if (cookieHeader.Length > 0) { arg.RequestContent.Headers[HttpRequestHeader.Cookie] = cookieHeader.Replace(',', ';'); } //WinInetInterop.SaveCookies(_cookieContainer, absoluteUri); } else { arg.RequestContent.Headers[HttpRequestHeader.Cookie] = string.Empty; //WinInetInterop.DeleteCache(WinInetInterop.CacheKind.Cookies); } if (arg.RequestContent.HasBody) { arg.RequestContent.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; postData = Encoding.UTF8.GetBytes(arg.RequestContent.GetFormString()); } headers = arg.RequestContent.GetHeadersString(); } browser.Navigate(arg.RequestUrl, "_self", postData, headers); TaskHelper.Factory.StartNew(this.STA_WaitSr, browser); this.Tag = arg; }
public string CurrentInvoke(string js, CurrentInvokeKind kind = CurrentInvokeKind.None) { this.CheckNavigated(); ScriptingContext arg = null; string result = MLA.Invoke(() => { arg = (ScriptingContext)browser.ObjectForScripting; if (kind == CurrentInvokeKind.Repost) { CurrentRepost(); } string callback = ClientForm.xInvoke(browser, js); if (kind == CurrentInvokeKind.AjaxEvent) { arg.SetAjax(browser, AjaxBlockFlags.Event, "CurrentInvoke"); } return(callback); }); if (arg == null) { throw new InvalidOperationException("CurrentInvoke NULL arg"); } switch (kind) { case CurrentInvokeKind.Repost: arg.WaitHandle.WaitOne(); break; case CurrentInvokeKind.AjaxEvent: arg.WaitAjax(this.SendReceiveTimeout, "CurrentInvoke"); break; } return(result); }
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { var browser = (WebBrowser)sender; #if DEBUG App.LogInfo("browser_DocumentCompleted {0}@{1}", e.Url, browser.Url); #endif var arg = (ScriptingContext)browser.ObjectForScripting; try { //e.Url不会变res:// if (!ScriptingContext.CheckDocument(browser.Url)) { App.LogInfo("browser_DocumentCompleted Cancel {0}", browser.Url); return; } if (browser.ReadyState != WebBrowserReadyState.Complete) { return; } //发生redirect或iframe load if (browser.Url != e.Url) { App.LogInfo("browser_DocumentCompleted Redirect {0} to {1}", arg.RequestUrl, e.Url); } if (this.UseCookies) { WinInetInterop.LoadCookies(_cookieContainer, browser.Document.Url); } ScriptingContext.InjectScript(browser.Document, @"if (typeof ($) == 'undefined') { var script = document.createElement('script'); script.src = 'http://libs.baidu.com/jquery/1.9.0/jquery.js'; document.getElementsByTagName('head')[0].appendChild(script); } function Soubiscbot(kind) { switch (kind) { case 0: var set = []; $(arguments[1]).each(function (i, o) { var me = $(o); var id = me.attr('id'); if (!id) { id = Math.random(); me.attr('id', id); } set[i] = id; }); return set.toString(); break; case 1: try { return arguments[1](); } catch (ex) { return ex.toString(); } break; default: return document.documentElement.outerHTML; break; } }"); if (this.SendReceiveTimeout != Timeout.Infinite) { arg.SendReceiveWaiter.Set(); } //EntryAjax arg.SetAjax(browser, AjaxBlockFlags.Block, arg.RequestUrl); arg.RegisterLazyLoad(x => { bool isSet = arg.WaitAjax(this.SendReceiveTimeout, arg.RequestUrl); _isNavigated = true; arg.WaitHandle.Set(); }, browser); } catch (Exception ex) { App.LogError(ex, "browser_DocumentCompleted RequestUrl={0} BrowserUrl={1}", arg.RequestUrl, browser.Url); } }