日期:2014-05-17  浏览次数:21051 次

发一个手动输入验证码自动补充账号密码登录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.Ch