下载远程验证码到本地

其实本质上就是把远程的图片Stream流下载到本地,用pictureBox展示出来而已

带上cookie,这样就可以肉眼识别

代码很简单,直接上了

前台也就是一个pictureBox控件

AutoLoginHelper autoLoginHelper=new AutoLoginHelper(Encoding.UTF8);
            CookieContainer cc=new CookieContainer();
            Stream stream=autoLoginHelper.GetImages("http://passport.csdn.net/ShowExPwd.aspx%22,string.Empty,Encoding.UTF8,ref cc);
            Image img = new Bitmap(stream);
            this.pictureBox1.Image = img;

public Stream GetImages(string url, string postData, Encoding encoding, ref CookieContainer cookieContainer)
        {
            byte[] postByte = encoding.GetBytes(postData);
            HttpWebRequest wr = System.Net.HttpWebRequest.Create(url) as HttpWebRequest;
            wr.CookieContainer = cookieContainer;
            wr.Method = "POST";
            wr.ContentType = "application/x-www-form-urlencoded";
            wr.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; MS-RTC LM 8; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            wr.Referer = url;
            wr.CookieContainer = cookieContainer;
            wr.ContentLength = postByte.Length;
            Stream sr = wr.GetRequestStream();
            sr.Write(postByte, 0, postByte.Length);
            sr.Close();
            HttpWebResponse response = wr.GetResponse() as HttpWebResponse;
            System.IO.Stream responseStream = response.GetResponseStream();
            return responseStream;
        }

你可能感兴趣的:(验证码)