winform WebBroswer示例(联通号码查询)
在其他几篇随笔中记录了WebBroswer的用法,这里就不再讲了,直接上传个示例(联通号码查询):
代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 mshtml;
using System.Security.Cryptography;
using System.Web;
namespace LianTong
{
public partial class ltform : Form
{
//WebService引用
private ltifi.Service1SoapClient ifiobj = new LianTong.ltifi.Service1SoapClient();
//查询表对象
private ltifi.SelectUnicom sUnicom = new LianTong.ltifi.SelectUnicom();
//程序运行状态标识
//判断是否刷屏
private int islock = 0;
//记录上一个号码
private string oruoldnum = "";
//timer2运行状态
private bool timer2b = false;
//验证信息
private string str = "12345678";
//号码
private string orunum = "";
//用户姓名
private string username = "";
//信息编号:
private string oruId = "";
//余额
private string remain = "";
//编号记录
private string oruoldId = "";
public ltform()
{
InitializeComponent();
}
private void toolStripLabel1_Click(object sender, EventArgs e)
{
webpage.Url = new Uri("http://192.168.3.60:8080/java_redirect.php");
}
private void toolStripLabel2_Click(object sender, EventArgs e)
{
if (cbbtime.Text == "")
{
MessageBox.Show("请选择间隔时间,推荐5000毫秒");
return;
}
if (toolStripLabel2.Text == "开始查询")
{
toolStripLabel2.Text = "停止查询";
timer1.Interval = Convert.ToInt32(cbbtime.Text.ToString().Trim());
timer1.Enabled = true;
}
else
{
toolStripLabel2.Text = "开始查询";
timer1.Enabled = false;
timer2b = false;
timer2.Enabled = false;
}
}
private void webpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webpage.ReadyState < WebBrowserReadyState.Complete)
return;
}
private void webpage_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
try
{
HtmlDocument doc = webpage.Document.Window.Frames["right_page"].Document.Window.Frames["cash_pay_query"].Document;
IHTMLWindow2 win = (IHTMLWindow2)doc.Window.DomWindow;
string s = "function confirm(str) {";
s += "return false;";
s += "}";
win.execScript(s, "javascript");
string s1 = "function alert(str) {return '';}";
win.execScript(s1, "javascript");
}
catch (Exception)
{
throw;
}
try
{
HtmlDocument doc = webpage.Document.Window.Frames["right_page"].Document.Window.Frames["cash_pay_result"].Document;
IHTMLWindow2 win = (IHTMLWindow2)doc.Window.DomWindow;
string s = "function confirm(str) {";
s += "return false;";
s += "}";
win.execScript(s, "javascript");
string s1 = "function alert(str) {return '';}";
win.execScript(s1, "javascript");
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 开始抓取数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
string strorder = "";
try
{
strorder = ifiobj.selectOneSelectStratMobile(Encrypt(), "1");
}
catch (Exception)
{
throw;
}
if (str == "")
{
return;
}
string[] strunicom = strorder.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
//订单编号
oruId = strunicom[0].ToString().Trim();
//订单号码
orunum = strunicom[1].ToString().Trim();
//订单金额
if (oruId != "0" && orunum != "0")
{
toolStripLabel5.Text = orunum + ":正在查询";
if (submit())
{
}
else
{
toolStripLabel5.Text = orunum + ":查询失败";
databind(oruId, orunum, "查询失败", "0", "查询失败", 9);
}
}
}
private bool submit()
{
try
{
HtmlDocument doc = webpage.Document.Window.Frames["right_page"].Document.Window.Frames["cash_pay_query"].Document;
HtmlElement serverid = doc.All["txt_serverid"];
HtmlElement sreach = doc.All["cmd_query"];
if (serverid == null || sreach == null)
{
return false;
}
serverid.SetAttribute("value", orunum);
timer1.Enabled = false;
islock = 0;
sreach.InvokeMember("click");
//System.Threading.Thread.Sleep(2000);
timer2b = true;
timer2.Enabled = true;
timer2.Interval = 1000;
}
catch (Exception)
{
throw;
}
return true;
}
/// <summary>
/// 读取页面信息判断是否成功
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer2_Tick(object sender, EventArgs e)
{
if (islock < 5)
{
HtmlDocument doc1 = webpage.Document.Window.Frames["right_page"].Document.Window.Frames["cash_pay_query"].Document;
HtmlElement reset = doc1.All["cmd_reset"];
try
{
string frm_result = webpage.Document.Window.Frames["right_page"].Document.Window.Frames["cash_pay_result"].Document.All[0].InnerText;
string[] strorder = frm_result.Split(new string[] { "话费余额", "用户信息" }, StringSplitOptions.RemoveEmptyEntries);
if (strorder.Length > 1)
{
string[] userinfo = strorder[1].ToString().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
string[] useri = strorder[0].ToString().Split(new string[] { "代理商账户", "帐户信息" }, StringSplitOptions.RemoveEmptyEntries);
string[] usern = useri[1].ToString().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
//对读取出的内容进行截取
username = usern[0].ToString();
remain = userinfo[userinfo.Length - 1];
oruoldnum = orunum;
toolStripLabel5.Text = orunum + ":查询成功";
databind(oruId, orunum, username, remain, "查询成功", 8);
oruoldId = oruId;
reset.InvokeMember("click");
timer2b = false;
timer1.Enabled = true;
timer2.Enabled = false;
islock++;
return;
}
islock++;
}
catch (Exception)
{
return;
}
}
else
{
HtmlDocument doc1 = webpage.Document.Window.Frames["right_page"].Document.Window.Frames["cash_pay_query"].Document;
HtmlElement reset = doc1.All["cmd_reset"];
timer2b = false;
toolStripLabel5.Text = orunum + ":查询失败";
databind(oruId, orunum, "查询失败", "0", "查询失败", 9);
reset.InvokeMember("click");
timer1.Enabled = true;
timer2.Enabled = false;
islock++;
}
}
private void ltform_Load(object sender, EventArgs e)
{
cbbtime.Text = "6000";
lvorder.Columns.Add("编号", 40);
lvorder.Columns.Add("号码", 80);
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;//是否可以选择行
this.Text = "联通号码查询";
toolStripLabel5.Text = orunum + "联通号码查询";
}
/// <summary>
/// 联通充值数据信息状态更新
/// </summary>
/// <param name="ormd"> 信息编号</param>
/// <param name="ormn">号码</param>
/// <param name="ormoney">金额</param>
/// <param name="state">状态</param>
/// <param name="n">状态标识</param>
private void databind(string ormd, string ormn, string name, string money, string state, int n)
{
bool id = false;
sUnicom.scuId = Convert.ToInt32(ormd.Trim());
sUnicom.scuName = name;
sUnicom.scuExecTime = DateTime.Now;
sUnicom.scuState = n;
sUnicom.scuRemainAmount = Convert.ToDecimal(money.ToString());
try
{
id = ifiobj.updateOneSelectUnicom(Encrypt(), sUnicom);
if (!id)
id = ifiobj.updateOneSelectUnicom(Encrypt(), sUnicom);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
ListViewItem[] listViewItem = new ListViewItem[1];
listViewItem[0] = new ListViewItem(new string[] { ormd, ormn, name, money + "元", state });
lvorder.Items.AddRange(listViewItem);
}
/// <summary>
/// 加密数据
/// </summary>
/// <returns></returns>
public static string Encrypt()
{
string Text = "asuygp834p8934g8ye893rehfvasu78698734r";
string sKey = "as4564656uygp834p893";
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
timer2b = false;
webpage.Url = new Uri("http://192.168.3.60:8080/java_redirect.php");
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mshtml;
using System.Security.Cryptography;
using System.Web;
namespace LianTong
{
public partial class ltform : Form
{
//WebService引用
private ltifi.Service1SoapClient ifiobj = new LianTong.ltifi.Service1SoapClient();
//查询表对象
private ltifi.SelectUnicom sUnicom = new LianTong.ltifi.SelectUnicom();
//程序运行状态标识
//判断是否刷屏
private int islock = 0;
//记录上一个号码
private string oruoldnum = "";
//timer2运行状态
private bool timer2b = false;
//验证信息
private string str = "12345678";
//号码
private string orunum = "";
//用户姓名
private string username = "";
//信息编号:
private string oruId = "";
//余额
private string remain = "";
//编号记录
private string oruoldId = "";
public ltform()
{
InitializeComponent();
}
private void toolStripLabel1_Click(object sender, EventArgs e)
{
webpage.Url = new Uri("http://192.168.3.60:8080/java_redirect.php");
}
private void toolStripLabel2_Click(object sender, EventArgs e)
{
if (cbbtime.Text == "")
{
MessageBox.Show("请选择间隔时间,推荐5000毫秒");
return;
}
if (toolStripLabel2.Text == "开始查询")
{
toolStripLabel2.Text = "停止查询";
timer1.Interval = Convert.ToInt32(cbbtime.Text.ToString().Trim());
timer1.Enabled = true;
}
else
{
toolStripLabel2.Text = "开始查询";
timer1.Enabled = false;
timer2b = false;
timer2.Enabled = false;
}
}
private void webpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webpage.ReadyState < WebBrowserReadyState.Complete)
return;
}
private void webpage_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
try
{
HtmlDocument doc = webpage.Document.Window.Frames["right_page"].Document.Window.Frames["cash_pay_query"].Document;
IHTMLWindow2 win = (IHTMLWindow2)doc.Window.DomWindow;
string s = "function confirm(str) {";
s += "return false;";
s += "}";
win.execScript(s, "javascript");
string s1 = "function alert(str) {return '';}";
win.execScript(s1, "javascript");
}
catch (Exception)
{
throw;
}
try
{
HtmlDocument doc = webpage.Document.Window.Frames["right_page"].Document.Window.Frames["cash_pay_result"].Document;
IHTMLWindow2 win = (IHTMLWindow2)doc.Window.DomWindow;
string s = "function confirm(str) {";
s += "return false;";
s += "}";
win.execScript(s, "javascript");
string s1 = "function alert(str) {return '';}";
win.execScript(s1, "javascript");
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 开始抓取数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
string strorder = "";
try
{
strorder = ifiobj.selectOneSelectStratMobile(Encrypt(), "1");
}
catch (Exception)
{
throw;
}
if (str == "")
{
return;
}
string[] strunicom = strorder.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);
//订单编号
oruId = strunicom[0].ToString().Trim();
//订单号码
orunum = strunicom[1].ToString().Trim();
//订单金额
if (oruId != "0" && orunum != "0")
{
toolStripLabel5.Text = orunum + ":正在查询";
if (submit())
{
}
else
{
toolStripLabel5.Text = orunum + ":查询失败";
databind(oruId, orunum, "查询失败", "0", "查询失败", 9);
}
}
}
private bool submit()
{
try
{
HtmlDocument doc = webpage.Document.Window.Frames["right_page"].Document.Window.Frames["cash_pay_query"].Document;
HtmlElement serverid = doc.All["txt_serverid"];
HtmlElement sreach = doc.All["cmd_query"];
if (serverid == null || sreach == null)
{
return false;
}
serverid.SetAttribute("value", orunum);
timer1.Enabled = false;
islock = 0;
sreach.InvokeMember("click");
//System.Threading.Thread.Sleep(2000);
timer2b = true;
timer2.Enabled = true;
timer2.Interval = 1000;
}
catch (Exception)
{
throw;
}
return true;
}
/// <summary>
/// 读取页面信息判断是否成功
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer2_Tick(object sender, EventArgs e)
{
if (islock < 5)
{
HtmlDocument doc1 = webpage.Document.Window.Frames["right_page"].Document.Window.Frames["cash_pay_query"].Document;
HtmlElement reset = doc1.All["cmd_reset"];
try
{
string frm_result = webpage.Document.Window.Frames["right_page"].Document.Window.Frames["cash_pay_result"].Document.All[0].InnerText;
string[] strorder = frm_result.Split(new string[] { "话费余额", "用户信息" }, StringSplitOptions.RemoveEmptyEntries);
if (strorder.Length > 1)
{
string[] userinfo = strorder[1].ToString().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
string[] useri = strorder[0].ToString().Split(new string[] { "代理商账户", "帐户信息" }, StringSplitOptions.RemoveEmptyEntries);
string[] usern = useri[1].ToString().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
//对读取出的内容进行截取
username = usern[0].ToString();
remain = userinfo[userinfo.Length - 1];
oruoldnum = orunum;
toolStripLabel5.Text = orunum + ":查询成功";
databind(oruId, orunum, username, remain, "查询成功", 8);
oruoldId = oruId;
reset.InvokeMember("click");
timer2b = false;
timer1.Enabled = true;
timer2.Enabled = false;
islock++;
return;
}
islock++;
}
catch (Exception)
{
return;
}
}
else
{
HtmlDocument doc1 = webpage.Document.Window.Frames["right_page"].Document.Window.Frames["cash_pay_query"].Document;
HtmlElement reset = doc1.All["cmd_reset"];
timer2b = false;
toolStripLabel5.Text = orunum + ":查询失败";
databind(oruId, orunum, "查询失败", "0", "查询失败", 9);
reset.InvokeMember("click");
timer1.Enabled = true;
timer2.Enabled = false;
islock++;
}
}
private void ltform_Load(object sender, EventArgs e)
{
cbbtime.Text = "6000";
lvorder.Columns.Add("编号", 40);
lvorder.Columns.Add("号码", 80);
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;//是否可以选择行
this.Text = "联通号码查询";
toolStripLabel5.Text = orunum + "联通号码查询";
}
/// <summary>
/// 联通充值数据信息状态更新
/// </summary>
/// <param name="ormd"> 信息编号</param>
/// <param name="ormn">号码</param>
/// <param name="ormoney">金额</param>
/// <param name="state">状态</param>
/// <param name="n">状态标识</param>
private void databind(string ormd, string ormn, string name, string money, string state, int n)
{
bool id = false;
sUnicom.scuId = Convert.ToInt32(ormd.Trim());
sUnicom.scuName = name;
sUnicom.scuExecTime = DateTime.Now;
sUnicom.scuState = n;
sUnicom.scuRemainAmount = Convert.ToDecimal(money.ToString());
try
{
id = ifiobj.updateOneSelectUnicom(Encrypt(), sUnicom);
if (!id)
id = ifiobj.updateOneSelectUnicom(Encrypt(), sUnicom);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
ListViewItem[] listViewItem = new ListViewItem[1];
listViewItem[0] = new ListViewItem(new string[] { ormd, ormn, name, money + "元", state });
lvorder.Items.AddRange(listViewItem);
}
/// <summary>
/// 加密数据
/// </summary>
/// <returns></returns>
public static string Encrypt()
{
string Text = "asuygp834p8934g8ye893rehfvasu78698734r";
string sKey = "as4564656uygp834p893";
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
timer2b = false;
webpage.Url = new Uri("http://192.168.3.60:8080/java_redirect.php");
}
}
}