c#抓取网页验证码

时间:2014-05-09 15:23:44   收藏:0   阅读:368

代码如下:

 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

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!