WinForm项目开发中WebBrowser用法实例汇总

时间:2021-05-20

本文实例汇总了WinForm项目开发中WebBrowser用法,希望对大家项目开发中使用WebBrowser起到一定的帮助,具体用法如下:

1.

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")][ComVisibleAttribute(true)]public partial class frmWebData : Form{public frmWebData(){ InitializeComponent();}protected override void OnLoad(EventArgs e){ wbService.ObjectForScripting = this; base.OnLoad(e);}}

2.后台调用Javascript脚本

<script type="text/javascript" language="javascript">function ErrorMessage(message) { document.getElementById("error").innerText = message;}</script>后台代码static string ErrorMsg = string.Empty;private void wbService_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e){ if (!string.IsNullOrEmpty(ErrorMsg)) wbService.Document.InvokeScript("ErrorMessage", new object[1] { string.Format("操作失败,原因:{0}!", ErrorMsg) });}

3.JavaScript脚本调用后台方法

脚本代码

<div id="content"> <h2 id="error"> 操作正在响应中.....</h2> <div class="utilities"> <a class="button right" onclick="window.external.DoSvrWebDbBack()">

刷新

</a> <!--<a class="button right" onclick="window.external.NavigateToLogin()">重新登录</a>--> <div class="clear"> </div> </div> </div>

后台代码

public void DoSvrWebDbBack(){ try { } catch (TimeoutException) { ErrorMsg = "超时,请稍候再尝试!"; } catch (Exception ex) { ErrorMsg = ex.Message.ToString(); }}

4.设置cookie

Cookie _cookie = BaseWinForm.LoginMessage.SessionID2; InternetSetCookie(BaseWinForm.LoginMessage.SvrWebDbBack, "ASP.NET_SessionId", _cookie.Value); wbService.Navigate(BaseWinForm.LoginMessage.SvrWebDbBack, null, null, string.Format("Referer:{0}", BaseWinForm.LoginMessage.SvrWebDbLoingUrl));[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]public static extern bool InternetSetCookie(string urlName, string cookieName, string cookieData);

5.请求链接获取返回处理

public class HttpWebRequestToolV2{public delegate HttpWebRequest RequestRule(string url);/// <summary>/// 发起HttpWebResponse请求/// </summary>/// <param name="url">请求连接</param>/// <param name="credentials">请求参数</param>/// <param name="httpWebRequestRule">请求设置『委托』,当委托等于NULL的时候,默认请求;否则使用所设置的HttpWebRequest</param>/// <returns>HttpWebResponse</returns>public static HttpWebResponse CreateHttpWebRequest(string url, byte[] credentials, RequestRule httpWebRequestRule){ if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url"); HttpWebRequest _request = null; if (httpWebRequestRule != null) { _request = httpWebRequestRule(url); } else { _request = WebRequest.Create(url) as HttpWebRequest; _request.Method = "POST"; _request.ContentType = "application/x-_SessionId"]; Cookie _err = _cookie["Err"]; if (_loginType != null && _err != null && sessionID != null) { _checkResult = _loginType.Value.Equals("1"); if (!_checkResult) _errorMessage = HttpUtility.UrlDecode(_err.Value); } else { _errorMessage = "Web服务异常,请稍候在试!"; } if (!string.IsNullOrEmpty(_errorMessage)) throw new Exception(_errorMessage); return _checkResult;}

6.从WebBrowser中获取CookieContainer

/// <summary>/// 从WebBrowser中获取CookieContainer/// </summary>/// <param name="webBrowser">WebBrowser对象</param>/// <returns>CookieContainer</returns>public static CookieContainer GetCookieContainer(this WebBrowser webBrowser){ if (webBrowser == null) throw new ArgumentNullException("webBrowser"); CookieContainer _cookieContainer = new CookieContainer(); string _cookieString = webBrowser.Document.Cookie; if (string.IsNullOrEmpty(_cookieString)) return _cookieContainer; string[] _cookies = _cookieString.Split(';'); if (_cookies == null) return _cookieContainer; foreach (string cookieString in _cookies) { string[] _cookieNameValue = cookieString.Split('='); if (_cookieNameValue.Length != 2) continue; Cookie _cookie = new Cookie(_cookieNameValue[0].Trim().ToString(), _cookieNameValue[1].Trim().ToString()); _cookieContainer.Add(_cookie); } return _cookieContainer;}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章