C#下用HttpWebRequest读取网页验证码图片并获取相关cookie
{
try
{
//验证码地址
Uri uri = new Uri("http://61.142.253.40:8085/CheckCode.aspx");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
//设置cookie,必须有下面这句代码,否则读不到返回的cookie
request.CookieContainer = new CookieContainer();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//读取key=ASP.NET_SessionId的cookie值
string Cookie = response.Cookies["ASP.NET_SessionId"].Value;
//得到验证码数据流
Stream resStream = response.GetResponseStream();
//初始化Bitmap图片并在pictureBox1控件中显示
Bitmap image = new Bitmap(resStream);
this.pictureBox1.Image = image;
}
catch { }
}