码迷,mamicode.com
首页 > Web开发 > 详细

c#抓取网页验证码

时间:2014-05-09 15:23:44      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:c#

代码如下:

 public partial class Form3 : Form
    {
        private CookieContainer cookie = new System.Net.CookieContainer();//声明一个全局cookie变量

        public Form3()
        {
            InitializeComponent();         
        }


        private void Form3_Load(object sender, EventArgs e)
        {
            Bind();
            this.label2.Cursor = System.Windows.Forms.Cursors.Hand;
        }

        private void Bind()
        {
            //读取验证码和cookie
            string URL = "https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand";

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
                request.CookieContainer = new CookieContainer();
                Stream responseStream = ((HttpWebResponse)request.GetResponse()).GetResponseStream();
                cookie = request.CookieContainer;
                string cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
                Image original = Image.FromStream(responseStream);
                Bitmap bitMap = new Bitmap(original);
                this.pictureBox1.Image = bitMap;
                responseStream.Close();
            }
            catch (Exception exception)
            {
                MessageBox.Show("ERROR:" + exception.Message);
            }
        }


        private void label2_Click(object sender, EventArgs e)
        {
            Bind();//刷新
        }
    }
}

bubuko.com,布布扣

c#抓取网页验证码,布布扣,bubuko.com

c#抓取网页验证码

标签:c#

原文地址:http://blog.csdn.net/lisenyang/article/details/25366171

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!