发一个手动输入验证码自动补充账号密码登录12306的东东[含源码]
如题……
非程序猿工作的业余爱好者……学了大半年没写点东西……
实现的效果是:手动输入验证码,不停的登录,以便登入订票后台……...
简单功能……高手见笑……
1.使用VS2010开发
2.需要引用MSHTML(Microsoft HTML Object Library)
源码链接:http://download.csdn.net/detail/geminizane/4001304
源码:
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;
namespace winFormYZM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static int TestCount = 1;//尝试次数
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textBox1.Text = webBrowser1.Document.Url.ToString();//文本框显示为当前地址
timer1.Start();//启动显示时间的计时器
textBox2.Focus();//每次提交后,页面都会重新载入,因此,将验证码的输入框获得焦点,以便输入
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate(new Uri("https://dynamic.12306.cn/otsweb/main.jsp"));//载入登录地址
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox3.Text) && !string.IsNullOrEmpty(textBox4.Text))
{
int intTxt = textBox2.Text.Length;
if (intTxt >= 5)//验证码为四位数,在输完四位数后,空格,增加一位,触发以下事件
{
string strCode = textBox2.Text.TrimEnd();//因为加了空格,所以去除末位的空格
webBrowser1.Document.Window.Frames[0].Document.GetElementById("UserName").SetAttribute("value", textBox3.Text);//账号输入框
webBrowser1.Document.Window.Frames[0].Document.GetElementById("password").SetAttribute("value", textBox4.Text);//密码输入框
webBrowser1.Document.Window.Frames[0].Document.GetElementById("randCode").SetAttribute("value", strCode);//验证码输入框
//webBrowser1.Document.Window.Frames[0].Document.InvokeScript("subForm();");
//webBrowser1.Document.Window.Frames[0].Document.GetElementById("subLink").InvokeMember("subForm");
HtmlElement str = webBrowser1.Document.Window.Frames[0].Document.GetElementById("subLink");//提交按钮
str.InvokeMember("click");//点击事件
textBox2.Text = "";//清空验证码文本框
label6.Text = "当前尝试次数:" + TestCount;//显示尝试次数
TestCount++;//尝试次数自增
}
}
else
{
MessageBox.Show("用户名/密码不能为空");
textBox2.Text = "";
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
webBrowser1.Refresh();//点击图片,刷新页面
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Refresh();//点击按钮,刷新页面
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)//是否显示验证码
{
if (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
return;
}
//this.pictureBox1.Image = GetRegCodePic(ref webBrowser1, "img_rrand_code", null, null);
this.pictureBox1.Image = GetRegCodePic(ref webBrowser1, "img_rrand_code", null, null);
}
else
{
pictureBox1.Image = null;
}
}
private void timer1_Tick(object sender, EventArgs e)//显示当前时间的计时器
{
label5.Text ="当前时间:"+DateTime.Now.ToString();
}
//感谢.NET交流群(67784580)中的各位朋友的源码倾情提供
public static Image GetRegCodePic(ref WebBrowser webBrowser, String imgID, String imgSrc, String imgAlt)
{
//HTMLDocument doc = (HTMLDocument)webBrowser.Document.DomDocument;
HTMLDocument doc = (HTMLDocument)webBrowser.Document.Window.Frames[0].Document.DomDocument;
HTMLBody body = (HTMLBody)doc.body;
IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
IHTMLControlElement img;
// 如果没有图片的ID,通过Src或Alt中的关键字来取
if (imgID.Length == 0)
{
Int32 ImgNum = GetPicIndex(ref webBrowser, ref imgSrc, ref imgAlt);
if (ImgNum == -1)
return null;
//img = (IHTMLControlElement)webBrowser.Document.Images[ImgNum].DomElement;
img = (IHTMLControlElement)webBrowser.Document.Window.Frames[0].Document.Images[ImgNum].DomElement;
}
else
{
//img = (IHTMLControlElement)webBrowser.Document.All[imgID].DomElement;
img = (IHTMLControlElement)webBrowser.Document.Window.Frames[0].Document.All[imgID].DomElement;
}
rang.add(img);
rang.execCommand("Copy", false, null);
Image regImg = Clipboard.GetImage();
Clipboard.Clear();
return regImg;
}
public static Int32 GetPicIndex(ref WebBrowser webBrowser, ref String imgSrc, ref String imgAlt)
{
IHTMLImgElement img;
// 获取所有的Image元素
//for (Int32 i = 0; i < webBrowser.Document.Images.Count; i++)
for (Int32 i = 0; i < webBrowser.Document.Window.Frames[0].Document.Images.Count; i++)
{
//img = (IHTMLImgElement)webBrowser.Document.Images[i].DomElement;
img = (IHTMLImgElement)webBrowser.Document.Window.Frames[0].Document.DomDocument;
if (imgAlt.Length == 0)
{
if (img.src.IndexOf(imgSrc) >= 0)
return i;
}
else
{
if (imgSrc.Length == 0)
{
// 当imgSrc为空时,只匹配imgAlt
if (img.alt.IndexOf(imgAlt) >= 0)
return i;
}
else
{
// 当imgSrc不为空时,匹配imgAlt和imgSrc任意一个
if (img.alt.IndexOf(imgAlt) >= 0 || img.src.IndexOf(imgSrc) >= 0)
return i;
}
}
}
return -1;
}
}
}