c# 获取验证码 163为例

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 Pop3MimeClient
{
    public partial class Form3 : Form
    {
        string name = string.Empty;
        string pwd = string.Empty;
        WebBrowser wb;
        HtmlDocument doc;
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }


        private void button1_Click(object sender, EventArgs e)
        {

            wb = new WebBrowser();
            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
            wb.Navigate("http://reg.email.163.com/unireg/call.do?cmd=register.entrance&from=126mail");
            textBox1.Text = "";
        }
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            doc = wb.Document;
            if (wb.ReadyState== WebBrowserReadyState.Interactive)
            {
                if (doc.Body.InnerText.IndexOf("注册字母邮箱") > 0)
                {
                    doc.GetElementById("tabsUl").Document.GetElementsByTagName("li")[0].FirstChild.InvokeMember("onclick");//默认选中字母邮箱注册选项
                    doc.GetElementById("nameIpt").Focus();
                    name = GetRandomStr_char(1, true) + GetRandomStr(17, true);
                    doc.GetElementById("nameIpt").InnerText = name;//首位必须是字母
                    pwd = GetRandomStr(16, true);
                    doc.GetElementById("mainPwdIpt").Focus();
                    doc.GetElementById("mainPwdIpt").InnerText = pwd;
                    doc.GetElementById("mainCfmPwdIpt").Focus();
                    doc.GetElementById("mainCfmPwdIpt").InnerText = pwd;
                    doc.GetElementById("mainRegA").Focus();
                    HTMLDocument docment = (HTMLDocument)wb.Document.DomDocument;
                    HTMLBody body = (HTMLBody)docment.body;
                    IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
                    IHTMLControlElement img;
                    img = (IHTMLControlElement)wb.Document.GetElementById("vcodeImg").DomElement;
                    rang.add(img);
                    rang.execCommand("Copy", false, null);
                    Image image = Clipboard.GetImage();
                    this.pictureBox1.Image = image;
                }
              
            }
            else if(wb.ReadyState== WebBrowserReadyState.Complete)
            {
                if (doc.Body.InnerText.IndexOf("您的注册信息正在处理中...") > 0)
                {
                    //int i = EDMSendAccountBLL.InsertNewAccount(name, pwd, int.Parse(Form1.type));
                    //申请太频繁ip被封或服务器繁忙
                    return;
                }
                if (doc.Body.InnerText.IndexOf("注册成功") > 0)
                {
                    //int i = EDMSendAccountBLL.InsertNewAccount(name + "@126.com", pwd, int.Parse(Form1.type)); //插入数据库
                    return;
                } 
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {       
            doc.GetElementById("mainRegA").InvokeMember("onclick");
            textBox1.Text = "";
        }
        /// <summary>
        /// 生成随机字母与数字
        /// </summary>
        /// <param name="Length">生成长度</param>
        /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
        /// <returns></returns>
        public static string GetRandomStr(int Length, bool Sleep)
        {
            if (Sleep)
                System.Threading.Thread.Sleep(3);
            char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            string result = "";
            int n = Pattern.Length;
            System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            for (int i = 0; i < Length; i++)
            {
                int rnd = random.Next(0, n);
                result += Pattern[rnd];
            }
            return result;
        }
        /// <summary>
        /// 生成随机纯字母随机数
        /// </summary>
        /// <param name="Length">生成长度</param>
        /// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
        /// <returns></returns>
        public static string GetRandomStr_char(int Length, bool Sleep)
        {
            if (Sleep) System.Threading.Thread.Sleep(3);
            char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            string result = "";
            int n = Pattern.Length;
            System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
            for (int i = 0; i < Length; i++)
            {
                int rnd = random.Next(0, n);
                result += Pattern[rnd];
            }
            return result;
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            button1_Click(null, null);
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            doc.GetElementById("vcodeIpt").InnerText = textBox1.Text.Trim().ToString();
            doc.GetElementById("vcodeIpt").Focus();
            textBox1.Focus();
        }
    }
}
原文地址:https://www.cnblogs.com/Iyce/p/3182209.html