c# 登录带验证码网站
前面提到怎样获取网站的临时会话id和如何获取网站的验证码,下面代码比较简单,直接利用得到的验证码登录网站
如果要实现完全不用人工参与直接登录带验证码的网站,需要验证码识别技术,以后有机会献上代码!
代码
CookieContainer cookieContainer = new CookieContainer();
string URI = "登录网址";
HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
string srcString = "";
// 接收返回的页面
HttpWebResponse response ;
System.IO.Stream responseStream;
//POST的数据
string postString = "username=*&password=*&passcode="+验证码+"&Submit=%D3%C3%BB%A7%B5%C7%C2%BC&comeurl=";
// 将提交的字符串数据转换成字节数组
byte[] postData = Encoding.ASCII.GetBytes(postString);
// 设置提交的相关参数
request = WebRequest.Create(URI) as HttpWebRequest;
request.Method = "POST";
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
cookieContainer.SetCookies(new Uri("服务器网址"), aspcookie);
request.CookieContainer = cookieContainer;
request.ContentLength = postData.Length;
// 接收返回的页面
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("GB2312"));
srcString = reader.ReadToEnd();