代码如下:

 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();//刷新
        }
    }
}



本文转载:CSDN博客