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;namespace AutoLogin
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} int step = 0;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement ClickBtn = null;
if (step==0)
{ HtmlDocument doc = webBrowser1.Document;
for (int i = 0; i < doc.All.Count; i++)
{ if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
{ switch (doc.All[i].Name)
{ case "userLoginId":
//name=userLoginId value="name"
doc.All[i].InnerText = "name"; // 用户名 break;
//value="password" name=password
case "password": doc.All[i].InnerText = "password"; // 密码
break;
//INPUT type=submit value=Submit
case "btnSubmit": ClickBtn = doc.All[i];
break;
}
if (doc.All[i].GetAttribute("type").ToLower().Equals("submit"))
{
ClickBtn = doc.All[i];
} }
}
if (ClickBtn != null)
{
ClickBtn.InvokeMember("Click"); // 点击“登录”按钮
} step = 1;
webBrowser1.Navigate("http://url");
}
if (step==1)
{
step = 2;
webBrowser1.Navigate("http://url");
}
}
private void button1_Click(object sender, EventArgs e)
{
string sUrl = txb_Url.Text.Trim(); if (sUrl.Length > 0)
{ webBrowser1.Navigate(sUrl);
}
} private void Form1_Load(object sender, EventArgs e)
{
button1_Click(sender, e);
}
}
}
namespace AutoLogin
{
partial class Form1
{
/// <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()
{
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.panel1 = new System.Windows.Forms.Panel();
this.txb_Url = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// webBrowser1
//
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(0, 0);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(719, 677);
this.webBrowser1.TabIndex = 2;
this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
//
// panel1
//
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.txb_Url);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(719, 24);
this.panel1.TabIndex = 3;
//
// txb_Url
//
this.txb_Url.Dock = System.Windows.Forms.DockStyle.Left;
this.txb_Url.Location = new System.Drawing.Point(0, 0);
this.txb_Url.Name = "txb_Url";
this.txb_Url.Size = new System.Drawing.Size(638, 21);
this.txb_Url.TabIndex = 0;
this.txb_Url.Text = "http://url";
//
// button1
//
this.button1.Dock = System.Windows.Forms.DockStyle.Right;
this.button1.Location = new System.Drawing.Point(644, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(719, 677);
this.Controls.Add(this.panel1);
this.Controls.Add(this.webBrowser1);
this.Name = "Form1";
this.Text = "auto login";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false); }
#endregion
private System.Windows.Forms.WebBrowser webBrowser1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txb_Url;
}
}
C# AutoLogin
原创
©著作权归作者所有:来自51CTO博客作者autumn的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C#:
C#中$的用法:"$"是在C#6.0出现的一个新特性,其作用相当于对String.format()的简化。s
字符串 c# 新特性 占位符 -
【C# 编程】C#事件(Event)
C# 中使用事件机制实现线程间的通信。 ...
事件机制 线程间的通信 c# 其他