C#远程登陆带验证码的网站

C#远程登陆带验证码的网站
第一步:添加空间引用

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Web;

第二步:按钮运行代码

private void button4_Click(object sender, System.EventArgs e)
  {
   this.button4.Enabled=false;
   //第一步:获取系统登陆验证码
   string url="http://localhost:8001/login.asp";
   string indata="";
   string outdata="";
   CookieContainer myCookieContainer=new CookieContainer();
   //新建一个CookieContainer来存放Cookie集合
   HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
   //新建一个HttpWebRequest
   myHttpWebRequest.Referer="http://localhost:8001/login.asp";
   myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
   myHttpWebRequest.ContentLength=indata.Length;
   myHttpWebRequest.Method="POST";
   myHttpWebRequest.CookieContainer=myCookieContainer;
   //设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
   Stream myRequestStream=myHttpWebRequest.GetRequestStream();
   StreamWriter myStreamWriter=new StreamWriter(myRequestStream,Encoding.GetEncoding("gb2312"));                
   myStreamWriter.Write(indata);
   //把数据写入HttpWebRequest的Request流
   myStreamWriter.Close();
   myRequestStream.Close();
   //关闭打开对象
   HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
   //新建一个HttpWebResponse
   myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
   //获取一个包含url的Cookie集合的CookieCollection
   Stream myResponseStream=myHttpWebResponse.GetResponseStream();
   StreamReader myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312"));
   outdata=myStreamReader.ReadToEnd();
   //把数据从HttpWebResponse的Response流中读出
   myStreamReader.Close();
   myResponseStream.Close();
   Console.WriteLine("获取验证码"+outdata);
   //第二步:尝试登陆系统获取登陆后的Cookie
   string VerifyCode=outdata.Substring(outdata.LastIndexOf("id=/"verifycode/"")+63,4);   
   url="http://localhost:8001/Checklogin.asp";
   indata="UserName=testuser&Password=testpassword&verifycode="+VerifyCode;
   outdata="";
   //新建一个CookieContainer来存放Cookie集合
   myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
   //新建一个HttpWebRequest
   myHttpWebRequest.Referer="http://localhost:8001/login.asp";
   myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
   myHttpWebRequest.ContentLength=indata.Length;
   myHttpWebRequest.Method="POST";
   myHttpWebRequest.CookieContainer=myCookieContainer;
   //设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
   myRequestStream=myHttpWebRequest.GetRequestStream();
   myStreamWriter=new StreamWriter(myRequestStream,Encoding.GetEncoding("gb2312"));                
   myStreamWriter.Write(indata);
   //把数据写入HttpWebRequest的Request流
   myStreamWriter.Close();
   myRequestStream.Close();
   //关闭打开对象
   myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
   //新建一个HttpWebResponse
   myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
   //获取一个包含url的Cookie集合的CookieCollection
   myResponseStream=myHttpWebResponse.GetResponseStream();
   myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312"));
   outdata=myStreamReader.ReadToEnd();
   //把数据从HttpWebResponse的Response流中读出
   myStreamReader.Close();
   myResponseStream.Close();
   Console.WriteLine("登陆系统"+outdata);
   //第二步:尝试需要登陆后才能浏览的页面
   //拿到登录后的Cookie,再发送需要登陆的页面请求就能直接读取到登录后的内容了
   url="http://localhost:8001/WowPrice.asp";
   myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
   myHttpWebRequest.CookieContainer=myCookieContainer;    
   //刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证
   myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
   myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
   myResponseStream=myHttpWebResponse.GetResponseStream();
   myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312"));
   outdata=myStreamReader.ReadToEnd();
   myStreamReader.Close();
   myResponseStream.Close();
   Console.WriteLine("获取价格页面"+outdata);
   this.button4.Enabled=true;
  }

posted on 2008-04-24 12:56  风灵溪清  阅读(170)  评论(0编辑  收藏  举报

导航