/// <summary>
/// 加载网页,模拟登录动作处理
/// </summary>
private void ProcessLogin()
{
// 验证配置文件
if (string.IsNullOrEmpty(url))
{
ShowMsg("配置文件错误");
return;
}
ShowMsg("正在加载登录网页...");
// 加载网页
webBrowser1.Navigate(url);
//等待浏览器控件加载完成
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
ShowMsg("加载完毕!");
//模拟登录事件
LoginSimulation(webBrowser1);
}
//模拟登录事件
private void LoginSimulation(WebBrowser wb)
{
try
{
ShowMsg(string.Format("账户名:[{0}],输入账户密码...", userName));
ShowMsg(string.Format("请确保配置文件中的用户及登录密码准确可用"));
// 网页元素
HtmlDocument doc = wb.Document;
HtmlElement emuser = doc.GetElementById("user");
SetHtmlValue(emuser, userName);//设置账户
HtmlElement empassword = doc.GetElementById("password");
SetHtmlValue(empassword, password);//设置密码
HtmlElement btn = doc.GetElementById("Login");
InvokeMethod(btn, "click");//调用 登录按钮的 Click 事件 提交配置
ShowMsg("完成!");
TimeSpan used = DateTime.Now - begin;//用时
ShowMsg(string.Format("用时: {0}.{1}s" , used.Seconds, used.Milliseconds));
ShowMsg("即将自动退出...");
//启动计时器,4s 后自动退出当前程序
Timer timer = new Timer();
timer.Interval = 4000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
catch (Exception ex)
{
ShowMsg(ex.Message);
}
}
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace LoginAssistant
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
readConfigFile();
ProcessLogin();
}
/// <summary>
/// 加载网页,模拟登录动作处理
/// </summary>
private void ProcessLogin()
{
// 验证配置文件
if (string.IsNullOrEmpty(url))
{
ShowMsg("配置文件错误");
return;
}
ShowMsg("正在加载登录网页...");
// 加载网页
webBrowser1.Navigate(url);
//等待浏览器控件加载完成
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
ShowMsg("加载完毕!");
//模拟登录事件
LoginSimulation(webBrowser1);
}
//模拟登录事件
private void LoginSimulation(WebBrowser wb)
{
try
{
ShowMsg(string.Format("账户名:[{0}],输入账户密码...", userName));
ShowMsg(string.Format("请确保配置文件中的用户及登录密码准确可用"));
// 网页元素
HtmlDocument doc = wb.Document;
HtmlElement emuser = doc.GetElementById("user");
SetHtmlValue(emuser, userName);//设置账户
HtmlElement empassword = doc.GetElementById("password");
SetHtmlValue(empassword, password);//设置密码
HtmlElement btn = doc.GetElementById("Login");
InvokeMethod(btn, "click");//调用 登录按钮的 Click 事件 提交配置
ShowMsg("完成!");
TimeSpan used = DateTime.Now - begin;//用时
ShowMsg(string.Format("用时: {0}.{1}s" , used.Seconds, used.Milliseconds));
ShowMsg("即将自动退出...");
//启动计时器,4s 后自动退出当前程序
Timer timer = new Timer();
timer.Interval = 4000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
catch (Exception ex)
{
ShowMsg(ex.Message);
}
}
//自动退出
void timer_Tick(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// 调用 Html 元素的方法
/// </summary>
/// <param name="em"></param>
/// <param name="methodname"></param>
private void InvokeMethod(HtmlElement em, string methodname)
{
if (em == null) return;
object response = em.InvokeMember(methodname); //触发submit事件
}
//赋值于 Html 元素
private void SetHtmlValue(HtmlElement em, string valueStr)
{
if (em == null) return;
em.SetAttribute("value", valueStr);
}
//读取配置文件
private void readConfigFile()
{
try
{
if(!File.Exists(fileName))return;
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
StreamReader m_streamReader = new StreamReader(fs);
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
string strLine = m_streamReader.ReadLine();
while (strLine != null)
{
string[] data = strLine.Split('=');
switch (data[0])
{
case "user":
userName = getValue(data);
break;
case "password":
password = getValue(data);
break;
case "url":
url = getValue(data);
break;
default:
break;
}
strLine = m_streamReader.ReadLine();
}
m_streamReader.Close();
fs.Close();
}
}
catch (Exception ex)
{
ShowMsg(ex.Message);
}
}
/// <summary>
/// 获取取配置文件节点值
/// </summary>
/// <param name="arrays"></param>
/// <returns></returns>
private string getValue(string[] arrays)
{
StringBuilder sb = new StringBuilder();
sb.Append(arrays[1]);
for (int i = 2; i < arrays.Length; i++)
{
sb.Append("=" + arrays[i]);
}
return sb.ToString();
}
/// <summary>
/// 显示信息
/// </summary>
/// <param name="p"></param>
private void ShowMsg(string p)
{
rtbStatus.AppendText(string.Format("[{0}] {1}\r\n",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), p));
}
#region variables
//账号、密码
private string userName = "allliangkaiyu"; // default
private string password = "vicky";
private string url = string.Empty; //登录页面
string fileName = "WirelessAssistantConfig.ini"; //配置文件名
WebBrowser webBrowser1 = new WebBrowser();//浏览器控件
private DateTime begin = DateTime.Now;//当前时刻
#endregion
#region 按钮事件
//登录
private void btnRegister_Click(object sender, EventArgs e)
{
ProcessLogin();
}
//退出
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
}
}
namespace LoginAssistant
{
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.btnInput = new System.Windows.Forms.Button();
this.rtbStatus = new System.Windows.Forms.RichTextBox();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnInput
//
this.btnInput.Font = new System.Drawing.Font("Consolas", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnInput.Location = new System.Drawing.Point(46, 235);
this.btnInput.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.btnInput.Name = "btnInput";
this.btnInput.Size = new System.Drawing.Size(86, 36);
this.btnInput.TabIndex = 0;
this.btnInput.Text = "重新登录";
this.btnInput.UseVisualStyleBackColor = true;
this.btnInput.Click += new System.EventHandler(this.btnRegister_Click);
//
// rtbStatus
//
this.rtbStatus.BackColor = System.Drawing.SystemColors.Control;
this.rtbStatus.Dock = System.Windows.Forms.DockStyle.Top;
this.rtbStatus.Location = new System.Drawing.Point(0, 0);
this.rtbStatus.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.rtbStatus.Name = "rtbStatus";
this.rtbStatus.Size = new System.Drawing.Size(322, 229);
this.rtbStatus.TabIndex = 1;
this.rtbStatus.Text = "";
//
// btnExit
//
this.btnExit.Font = new System.Drawing.Font("Consolas", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnExit.ForeColor = System.Drawing.Color.Red;
this.btnExit.Location = new System.Drawing.Point(150, 235);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 36);
this.btnExit.TabIndex = 2;
this.btnExit.Text = "退出";
this.btnExit.UseVisualStyleBackColor = true;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(322, 274);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.rtbStatus);
this.Controls.Add(this.btnInput);
this.Font = new System.Drawing.Font("Consolas", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
this.Name = "MainForm";
this.Text = "无线网络助手 V20160908© vicky";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button btnInput;
private System.Windows.Forms.RichTextBox rtbStatus;
private System.Windows.Forms.Button btnExit;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有