前面已经讲过了WebBrowser的简单用法,下面依一个具体的应用实例来说明下WebBrowser:

这个实例是以读取移动交费中间商连连的交费页面进行自动交费的应用,其整体思路是从已有的数据库中读取数据做为交费信息连续像移动官方提交数据:

一、首先在winform窗体中拖入WebBrowser控件命名为webPage并设置其屏蔽js脚本错误的属性。在load事件中:



this.webPage.Navigate("连连官方交费网址");



这样当页面打开我们就进入了连连官方交费网站的登录页面。输入用户信息和密码,相关的验证信息即可进入个人页面。

二、加入一个按钮当我们单击这个按钮时就开始程序进行交费,这个过程我用了一个时间线程控件命名为timer1.其单击事件代码:

代码
if (tsbtnstart.Text == "开始")
{
timer1.Enabled = true;
t2 = true;
tsbtnstart.Text = "停止";
t11 = true;
}
else
{
timer1.Enabled = false;
tsbtnstart.Text = "开始";
t2 = false;
t11 = false;
}

其中t2,t11分别为两个bool变量做为timer1是否开启的标识,下面会用到,这里不再多说。现在timer1已经开始读取数据,下面是timer1执行的事件:

代码
private void timer1_Tick(object sender, EventArgs e)
{
try
{
//如果timer2正在运行
while (timer2.Enabled)
{
System.Threading.Thread.Sleep(500);
};
}
catch (Exception)
{

}
//充值提交运行结果
string state = "";
//数据库信息
string orderstr = "";

try
{
//从数据库中读取充值信息,并完成扣费,将其状态改为正在充值,并返回该条信息的编号,号码,金额用"_"隔开
orderstr = ifiobj.goulstrll(str1);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//如果数据不为空
if (orderstr != "")
{
//将结果截取成数组
string[] strorder = orderstr.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
if (strorder.Length == 3)
{
//编号
ormid = strorder[0].ToString();
//号码
ormnum = strorder[1].ToString();
//金额
ormmoney = strorder[2].ToString();
}
//如果没有数据
if (ormid == "0" && ormnum == "0")
{
return;
}
//如果提交该条数据的用户余额不足
else if (ormid == "0" && ormnum == "1")
{
return;
}
else
{
//调用远程页面提交数据充值
state = submittoweb(ormid, ormnum, ormmoney);
//如果返回结果为over则中断程序

if (state == "over")
{
timer1.Enabled = false;
t2 = false;
t22 = false;
MessageBox.Show("号码:" + ormnum + "充值:" + ormmoney + "元失败,由于已经提前扣费,系统已经终止运行,请对提交该业务的营业厅进行返账操作后,点击确定继续运行。", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
timer1.Enabled = true;
t2 = true;
t22 = true;
return;
}
else if (state == "continue")
{
//timer2开启,timer2关闭
islock = 1;
timer1.Enabled = false;
t22 = true;
t11 = false;
timer2.Enabled = true;
}

else
{
databind(ormid, ormnum, ormmoney, "充值失败", 0);
return;
}
//}
}
}
}

这段代码中出现了timer2时间控件当timer1已经成功提交了一笔交费信息时关闭它,并同时打开timer2对页面进行不间断的读取当获得成功反馈信息时将timer1提交的数据记录为成功否则失败。其中的t22和t11一样是用来标识timer2的。下面是对提取的数据进行网页提交submittoweb函数:

代码
private string submittoweb(string ormid1,string ormnum1, string ormmoney1)
{
try
{
//页面元素读取,读取框架tabWin中的内容
HtmlDocument doc = webPage.Document.Window.Frames["tabWin"].Document;
//用户号码
HtmlElement usernum = doc.All["custsimno"];
//重复输入
HtmlElement usernum1 = doc.All["custsimno1"];
//充值金额
HtmlElement usermoney = doc.All["pay"];
//代理商密码
HtmlElement userpwd = doc.All["agtpwd"];
//提交按钮
HtmlElement formbtn = doc.All["confirm"];
if (usernum == null || usermoney == null || formbtn == null || usernum1 == null || userpwd == null)
{
//如果页面读取出错
databind(ormid, ormnum, ormmoney, "连连充值失败提交冲正", 0);
return "continue";
}
//对页面内容进行赋值
usernum.SetAttribute("value", ormnum);
usernum1.SetAttribute("value", ormnum);
usermoney.SetAttribute("value", ormmoney);
userpwd.SetAttribute("value", str);
//执行提交按钮
formbtn.InvokeMember("click");
//执行js代码
doc.InvokeScript("SubmitbtnSave");
}
catch
{

}
return "continue";
}

这段代码就不用多讲了接着timer1的事件,当执行完上面这段代码后如果顺利完成就返回:continue字符从面启动timer2关闭timer1否则返回over结束timer1等待人式处理

下面是timer2的事件代码:

代码
private void timer2_Tick(object sender, EventArgs e)
{
//如果timer1正在运行
if (timer1.Enabled)
{
//timer2.Enabled = false;
return;
}
string innerText = "";
try
{
//获取页面集合
HtmlDocument doc = webPage.Document.Window.Frames["tabWin"].Document;
//读取页面form中的内容
innerText = doc.GetElementById("frmEdit").InnerText;
}
catch
{

throw;
}
//对读取出的内容进行截取
string[] strorder = innerText.Split(new string[] { "用户姓名", "充值金额", "代理钱包余额", "冻结钱包余额" }, StringSplitOptions.RemoveEmptyEntries);

if (strorder.Length <5)
{
return;
}
else
{
strorder[4].Replace(" ", "");
//如果剩余金额不为空
if (strorder[4].ToString() != "\r\n")
{
strorder[2].Replace("\r\n", "");
strorder[2].Replace(" ", "");
strorder[4].Replace(" ", "");
if (strorder[4].ToString() == "\r\n"||strorder[2].ToString()=="")
return;
if (allmoney !=strorder[4].ToString())
{
strorder[4].Replace("\r\n", "");

strorder[4].Replace(",", "");
strorder[4].Replace(",", "");

//记录下用户名和金额
allmoney = strorder[4].ToString();
username = strorder[2].ToString();
//交易计数
nums++;
//进行数据记录,显示在listview中
databind(ormid, ormnum, ormmoney, "充值成功", 1);
//timer2关闭
timer2.Enabled = false;
//timer2关闭
//islock = 0;
t22 = false;
t11 = true;
timer1.Enabled = true;
}
else
return;
}
else
return;
}
}

这样整体的流程就完成了,可是当我们人工提交充值信息成功时官方会从服务器返回一个js的成功提示框,这样整个程序将被终止,如何处理,在网上看了好多例子结果没有合适的,就自己试了一下,着手点还是webBrowser控件的两个事件不再多说,前面已经讲过:



代码
private void webPage_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
//屏蔽js窗体
if (islock==1)
{
try
{
HtmlDocument doc = webPage.Document.Window.Frames["tabWin"].Document;
IHTMLWindow2 win = (IHTMLWindow2)doc.Window.DomWindow;
string s = "function alert() {return '';}";
win.execScript(s, "javascript");
}
catch
{


}

}

count++;
}



代码
private void webPage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webPage.ReadyState < WebBrowserReadyState.Complete)
return;
////屏蔽js窗体
if (webPage.DocumentText.IndexOf("window.open(") > -1 || webPage.DocumentText.IndexOf("window.close()") > -1)
{
webPage.DocumentText = webPage.DocumentText.Replace("window.open(", "window.external.open(").Replace("window.close()", "window.external.close()");
}
try
{
HtmlDocument doc = webPage.Document.Window.Frames["tabWin"].Document;
IHTMLWindow2 win = (IHTMLWindow2)doc.Window.DomWindow;
string s = "function confirm() {";
s += "return true;";
s += "}";
s += "function alert(str) {return '';}";
win.execScript(s, "javascript");
string s1 = "window.alert = null;\r\nwindow.confirm = null;\r\nwindow.open = null;\r\nwindow.showModalDialog = null;";
win.execScript(s1, "javascript");
}
catch
{

throw;
}

count--;
//判断页面是否加载完成count=0
if (count == 0 && t2)
{
//开启timer进行数据库信息读取
if (t11)
{
if (timer1.Enabled && timer2.Enabled)
timer2.Enabled = false;
else if (timer1.Enabled == false && timer2.Enabled == false)
{
timer1.Enabled = true;
}
else
{
timer1.Enabled = true;
}
}
//开启timer2进行页面信息读取
else if (t22)
{
if (timer1.Enabled && timer2.Enabled)
timer1.Enabled = false;
else if (timer1.Enabled == false && timer2.Enabled == false)
{
timer2.Enabled = true;
}
else
{
timer2.Enabled = true;
}
}
else
{

}
}
}这两段代码中有些冗余,没有清除,如果需要可以自己试着清除。

完整代码:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using mshtml;

namespace Demo
{
public partial class ForthForm : Form
{
llifi.Service1SoapClient ifiobj = new Demo.llifi.Service1SoapClient();
//验证
private string str1 = "*******";
//代理密码
private string str = "******";
//如果交费成功屏蔽js标识
private int islock = 0;
//账户余额
private string allmoney = "\r\n";
//充值用户
private string username = "";
//交易数量
private int nums = 0;
//页面刷新完成标识
private int count = 0;
//timer1标识
private bool t11 = false;
//程序是否开启标识
private bool t2 = true;
//timer2标识
private bool t22 = false;
//交易编号
private string ormid = "";
//交易号码
private string ormnum = "";
//交易金额
private string ormmoney = "";
public ForthForm()
{
InitializeComponent();

}

private void tsbtnNavigate_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.tscboURL.Text))
{
MessageBox.Show("No URL was checked!");
return;
}
else if (this.tscboURL.Text == "河南连连科技")
{
this.webPage.Navigate("l连连网");
tsbtnstart.Enabled = true;
}
else
{

}
}

private void toolStripButton1_Click(object sender, EventArgs e)
{
if (tsbtnstart.Text == "开始")
{
timer1.Enabled = true;
t2 = true;
tsbtnstart.Text = "停止";
t11 = true;
}
else
{
timer1.Enabled = false;
tsbtnstart.Text = "开始";
t2 = false;
t11 = false;
}
}

private void timer1_Tick(object sender, EventArgs e)
{
try
{
//如果timer2正在运行
while (timer2.Enabled)
{
System.Threading.Thread.Sleep(500);
};
}
catch (Exception)
{

}
//充值提交运行结果
string state = "";
//数据库信息
string orderstr = "";

try
{
//从数据库中读取充值信息,并完成扣费,将其状态改为正在充值,并返回该条信息的编号,号码,金额用"_"隔开
orderstr = ifiobj.goulstrll(str1);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//如果数据不为空
if (orderstr != "")
{
//将结果截取成数组
string[] strorder = orderstr.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
if (strorder.Length == 3)
{
//编号
ormid = strorder[0].ToString();
//号码
ormnum = strorder[1].ToString();
//金额
ormmoney = strorder[2].ToString();
}
//如果没有数据
if (ormid == "0" && ormnum == "0")
{
return;
}
//如果提交该条数据的用户余额不足
else if (ormid == "0" && ormnum == "1")
{
return;
}
else
{
//调用远程页面提交数据充值
state = submittoweb(ormid, ormnum, ormmoney);
//如果返回结果为over则中断程序

if (state == "over")
{
timer1.Enabled = false;
t2 = false;
t22 = false;
MessageBox.Show("号码:" + ormnum + "充值:" + ormmoney + "元失败,由于已经提前扣费,系统已经终止运行,请对提交该业务的营业厅进行返账操作后,点击确定继续运行。", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
timer1.Enabled = true;
t2 = true;
t22 = true;
return;
}
else if (state == "continue")
{
//timer2开启,timer2关闭
islock = 1;
timer1.Enabled = false;
t22 = true;
t11 = false;
timer2.Enabled = true;
}

else
{
databind(ormid, ormnum, ormmoney, "充值失败", 0);
return;
}
//}
}
}
}
private string submittoweb(string ormid1,string ormnum1, string ormmoney1)
{
try
{
//页面元素读取,读取框架tabWin中的内容
HtmlDocument doc = webPage.Document.Window.Frames["tabWin"].Document;
//用户号码
HtmlElement usernum = doc.All["custsimno"];
//重复输入
HtmlElement usernum1 = doc.All["custsimno1"];
//充值金额
HtmlElement usermoney = doc.All["pay"];
//代理商密码
HtmlElement userpwd = doc.All["agtpwd"];
//提交按钮
HtmlElement formbtn = doc.All["confirm"];
if (usernum == null || usermoney == null || formbtn == null || usernum1 == null || userpwd == null)
{
//如果页面读取出错
databind(ormid, ormnum, ormmoney, "连连充值失败提交冲正", 0);
return "continue";
}
//对页面内容进行赋值
usernum.SetAttribute("value", ormnum);
usernum1.SetAttribute("value", ormnum);
usermoney.SetAttribute("value", ormmoney);
userpwd.SetAttribute("value", str);
//执行提交按钮
formbtn.InvokeMember("click");
//执行js代码
doc.InvokeScript("SubmitbtnSave");
}
catch
{

}
return "continue";
}

private void timer2_Tick(object sender, EventArgs e)
{
//如果timer1正在运行
if (timer1.Enabled)
{
//timer2.Enabled = false;
return;
}
string innerText = "";
try
{
//获取页面集合
HtmlDocument doc = webPage.Document.Window.Frames["tabWin"].Document;
//读取页面form中的内容
innerText = doc.GetElementById("frmEdit").InnerText;
}
catch
{

throw;
}
//对读取出的内容进行截取
string[] strorder = innerText.Split(new string[] { "用户姓名", "充值金额", "代理钱包余额", "冻结钱包余额" }, StringSplitOptions.RemoveEmptyEntries);

if (strorder.Length <5)
{
return;
}
else
{
strorder[4].Replace(" ", "");
//如果剩余金额不为空
if (strorder[4].ToString() != "\r\n")
{
strorder[2].Replace("\r\n", "");
strorder[2].Replace(" ", "");
strorder[4].Replace(" ", "");
if (strorder[4].ToString() == "\r\n"||strorder[2].ToString()=="")
return;
if (allmoney !=strorder[4].ToString())
{
strorder[4].Replace("\r\n", "");

strorder[4].Replace(",", "");
strorder[4].Replace(",", "");

//记录下用户名和金额
allmoney = strorder[4].ToString();
username = strorder[2].ToString();
//交易计数
nums++;
//进行数据记录,显示在listview中
databind(ormid, ormnum, ormmoney, "充值成功", 1);
//timer2关闭
timer2.Enabled = false;
//timer2关闭
//islock = 0;
t22 = false;
t11 = true;
timer1.Enabled = true;
}
else
return;
}
else
return;
}
}

private void webPage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webPage.ReadyState < WebBrowserReadyState.Complete)
return;
////屏蔽js窗体
if (webPage.DocumentText.IndexOf("window.open(") > -1 || webPage.DocumentText.IndexOf("window.close()") > -1)
{
webPage.DocumentText = webPage.DocumentText.Replace("window.open(", "window.external.open(").Replace("window.close()", "window.external.close()");
}
try
{
HtmlDocument doc = webPage.Document.Window.Frames["tabWin"].Document;
IHTMLWindow2 win = (IHTMLWindow2)doc.Window.DomWindow;
string s = "function confirm() {";
s += "return true;";
s += "}";
s += "function alert(str) {return '';}";
win.execScript(s, "javascript");
string s1 = "window.alert = null;\r\nwindow.confirm = null;\r\nwindow.open = null;\r\nwindow.showModalDialog = null;";
win.execScript(s1, "javascript");
}
catch
{

throw;
}

count--;
//判断页面是否加载完成count=0
if (count == 0 && t2)
{
//开启timer进行数据库信息读取
if (t11)
{
if (timer1.Enabled && timer2.Enabled)
timer2.Enabled = false;
else if (timer1.Enabled == false && timer2.Enabled == false)
{
timer1.Enabled = true;
}
else
{
timer1.Enabled = true;
}
}
//开启timer2进行页面信息读取
else if (t22)
{
if (timer1.Enabled && timer2.Enabled)
timer1.Enabled = false;
else if (timer1.Enabled == false && timer2.Enabled == false)
{
timer2.Enabled = true;
}
else
{
timer2.Enabled = true;
}
}
else
{

}
}
}

private void webPage_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
//屏蔽js窗体
if (islock==1)
{
try
{
HtmlDocument doc = webPage.Document.Window.Frames["tabWin"].Document;
IHTMLWindow2 win = (IHTMLWindow2)doc.Window.DomWindow;
string s = "function alert() {return '';}";
win.execScript(s, "javascript");
}
catch
{


}

}

count++;
}
/// <summary>
/// 连连充值数据信息状态更新
/// </summary>
/// <param name="ormd"> 信息编号</param>
/// <param name="ormn">号码</param>
/// <param name="ormoney">金额</param>
/// <param name="state">状态</param>
/// <param name="n">状态标识,1成功,0失败</param>
private void databind(string ormd, string ormn, string ormoney, string state, int n)
{
int id = 0;
try
{
id = ifiobj.SetOrderMobileStatell(str1, Convert.ToInt32(ormd), n, "连连交费" + state);
}
catch
{

}
if (id == 1)
{
ListViewItem[] listViewItem = new ListViewItem[1];
listViewItem[0] = new ListViewItem(new string[] { ormd, ormn, ormoney + "元", state });
lvorder.Items.AddRange(listViewItem);
////开启timer1标识
//t11 = true;
//timer1.Enabled = true;
return;
}
else
{
t2 = false;
timer1.Enabled = false;
MessageBox.Show("编号为:" + ormid + "的交易" + state + "\r\n信息记录失败,程序已经终止,请记录该信息,单击确定继续程序", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
t2 = true;
timer1.Enabled = true;
}
}
private void ForthForm_Load(object sender, EventArgs e)
{
tsbtnstart.Enabled = false;
lvorder.Columns.Add("编号", 40);
lvorder.Columns.Add("号码", 80);
lvorder.Columns.Add("充值金额", 80);
lvorder.Columns.Add("状态", 120);
lvorder.GridLines = true; //显示表格线
lvorder.View = View.Details;//显示表格细节
lvorder.HeaderStyle = ColumnHeaderStyle.Clickable;//对表头进行设置
lvorder.FullRowSelect = true;//是否可以选择行
}


}
}
posted on 2010-12-08 11:24  也风  阅读(2532)  评论(1编辑  收藏  举报