C#抓取需要登录的页面

抓取需要登录的页面的内容,原理主要是:先模拟登录,获取到COOKIE,然后接下来的访问,都使用这个COOKIE,就可以访问到需要登录的页面。

理论上,浏览器可以做到的事情,程序应该也可以。

不过,模拟登录,说起来容易,但不同站点有不同的处理方式,复杂程度不同。

1、最简单的,是POST适当的数据,不用验证码

2、象Discuz!系列的,要先访问某一个页面,获得随机码,然后置于POST数据中,才可以登录

3、需要验证码。验证码识别是另外一个课题。

无论如何,POST数据必须的。那么,模拟登录,需要POST哪些数据呢?

其实,需要POST哪些数据,每个站点都有所不同,所以要有一个合适的工具来进行分析。我装的是firefox的扩展控件:HttpFox。利用它,可以很方便的获取登录指定站点时,所需要提交的数据串。

模拟登录的代码是这样的:

//sPostData,待提交的数据串,如何http://www.test.com/login.aspx?user=admin&pwd=123456 public static CookieContainer Login(string url, string sPostData, CookieContainer cc) { CookieContainer container = (cc == null) ? new CookieContainer() : cc; ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(sPostData); HttpWebRequest resquest = ResquestInit(url); resquest.Method = "POST"; resquest.ContentLength = data.Length; resquest.CookieContainer = container; Stream newStream = resquest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); try { HttpWebResponse response = (HttpWebResponse)resquest.GetResponse(); response.Cookies = container.GetCookies(resquest.RequestUri); } catch{} return container; } //这个函数的作用就是统一Request的格式,使得每次访问目标网站都用相同的口径。如果参数不同的话,可能造成COOKIE无效,因而登录无效 public static HttpWebRequest ResquestInit(string url) { Uri target = new Uri(url); HttpWebRequest resquest = (HttpWebRequest)WebRequest.Create(target); resquest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2 (.NET CLR 3.5.30729)"; resquest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; resquest.AllowAutoRedirect = true; resquest.KeepAlive = true; resquest.ReadWriteTimeout = 120000; resquest.ContentType = "application/x-www-form-urlencoded"; resquest.Referer = url; return resquest; }


获得这个CookieContainer后,保存下来,以后每访问该网站,都带上它。CookieContainer相当于浏览器的COOKIE容器,里面存放访问各个网站的COOKIE。

带COOKIE访问代码如下:

//获得response static HttpWebResponse GetResponse(string url, CookieContainer cc) { try { HttpWebRequest resquest = ResquestInit(url); resquest.CookieContainer = cc; HttpWebResponse response = (HttpWebResponse)resquest.GetResponse(); response.Cookies = container.GetCookies(resquest.RequestUri); return response; } catch { return null; } }其中参数 CookieContainer cc 就是保存的CookieContainer。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值