使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路:

第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest;
第二 模拟POST或者GET方式提交的数据;
第三 模拟请求的头;
第四 提交请求并获得响应,及对响应做我们所需要的处理。
这里我们以人人网的登录为例,将涉及到POST以及GET两种请求方式。
大家使用抓包工具(IE调试工具/httpwatch)都是可以的,我这里采用httpwatch,登陆人人网的时候(www.renren.com),一共做了一个POST请求以及两个GET请求,如下图:

post了一个后,第一个返回状态值是200的一般就是登录后的首页地址,有些网页需要跳转的比较多一些,但是方法都是一样的,

观察这三个请求的详细信息,不难看出这里都是顺序的,第一个GET请求的地址由POST的响应得到,而第二个GET请求的地址又由第一个GET的响应得到。

每次请求与下一次请求之间的联系就是每次请求后返回的Cookies数据,前一次的返回Cookie数据需要同下一次请求一同发送到服务器,这也是C#模拟网站登陆的关键。

这里需要注意几点:

一、选择需要post的地址,可以通过工具查看获得,也可以通过查看网页源代码获得。

二、content可以查看返回的内容,或者是包含下一跳的链接地址。到最后一定是首页的网页内容。

先来模拟第一个POST请求

  1. HttpWebRequest request = null;
  2. HttpWebResponse response = null;
  3. string gethost = string.Empty;
  4. CookieContainer cc = new CookieContainer();
  5. string Cookiesstr = string.Empty;
  6. try
  7. {
  8. //第一次POST请求
  9. string postdata =“”email=adm13956587&password=786954887&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&captcha_type=web_login" //模拟请求数据,httpwatch中点击stream,就可以直接复制了
  10. string  LoginUrl="http://www.renren.com/PLogin.do";
  11. request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类
  12. request.Method = "POST";//数据提交方式为POST
  13. //模拟头
  14. request.ContentType = "application/x-www-form-urlencoded";
  15. byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
  16. request.ContentLength = postdatabytes.Length;
  17. request.AllowAutoRedirect = false;
  18. request.CookieContainer = cc;
  19. request.KeepAlive = true;
  20. //提交请求
  21. Stream stream;
  22. stream = request.GetRequestStream();
  23. stream.Write(postdatabytes, 0, postdatabytes.Length);
  24. stream.Close();
  25. //接收响应
  26. response = (HttpWebResponse)request.GetResponse();
  27. //保存返回cookie
  28. response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
  29. CookieCollection cook = response.Cookies;
  30. string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
  31. Cookiesstr = strcrook;
  32. //从返回的stream当中取第一次GET跳转地址: The URL has moved <a href="http://www.renren.com/home">here</a>
  33. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  34. string content = sr.ReadToEnd();
  35. response.Close();
  36. string[] substr = content.Split(new char[] { '"' });
  37. gethost = substr[1];   //http://www.renren.com/home
  38. }
  39. catch (Exception)
  40. {
  41. //第一次POST出错;
  42. }

注释写的很详细了,在这就不再分析,也许有人对request = (HttpWebRequest)WebRequest.Create(LoginUrl)有疑问,可以去google一下HttpWebRequest和WebRequest的区别,简单来说WebRequest是一个抽象类,不能直接实例化,需要被继承,而HttpWebRequest继承自WebRequest。

再模拟第一个和第二个GET请求

  1. try
  2. {
  3. request = (HttpWebRequest)WebRequest.Create(gethost);
  4. request.Method = "GET";
  5. request.KeepAlive = true;
  6. request.Headers.Add("Cookie:" + Cookiesstr);
  7. request.CookieContainer = cc;
  8. request.AllowAutoRedirect = false;
  9. response = (HttpWebResponse)request.GetResponse();
  10. //设置cookie
  11. Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
  12. //取再次跳转链接   The URL has moved <a href="http://www.renren.com/1915651750">here</a>
  13. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  14. string ss = sr.ReadToEnd();
  15. string[] substr = ss.Split(new char[] { '"' });
  16. gethost = substr[1];   //http://www.renren.com/1915651750
  17. request.Abort();
  18. sr.Close();
  19. response.Close();
  20. }
  21. catch (Exception)
  22. {
  23. //第一次GET出错
  24. }
  25. try
  26. {
  27. //第二次GET请求
  28. request = (HttpWebRequest)WebRequest.Create(gethost);
  29. request.Method = "GET";
  30. request.KeepAlive = true;
  31. request.Headers.Add("Cookie:" + Cookiesstr);
  32. request.CookieContainer = cc;
  33. request.AllowAutoRedirect = false;
  34. response = (HttpWebResponse)request.GetResponse();
  35. //设置cookie
  36. Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
  37. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

  38. string ss = sr.ReadToEnd();

  39. webBrowser1.Navigate("about:blank");
  40. webBrowser1.Document.OpenNew(true);

  41. webBrowser1.Document.Write(ss);

  42. request.Abort();
  43. response.Close();
  44. }
  45. catch (Exception)
  46. {
  47. //第二次GET出错
  48. }

GET与POST请求大同小异,这里便不再累述。三次请求结束,保存好你的cookie string,每次请求的时候都赋给请求的头部,你就处于登录状态了。

最新文章

  1. Castle中AdditionalInterfaces用法介绍
  2. H5学习
  3. 。U盘安装windows7操作系统
  4. 全端开发必备!10个最好的 Node.js MVC 框架
  5. 3.struts2接收页面传参的三种方式
  6. hadoop rpc基础
  7. yii2-user
  8. 【CC评网】2013.第41周 不求排版,简单就好
  9. BZOJ3996 [TJOI2015]线性代数
  10. phpcms 源码分析二:
  11. Borg Maze
  12. xp硬盘安装Fedora14 过程记录及心得体会(fedora14 live版本680M 和fedora14 DVD版本3.2G的选择)
  13. Lua 脚本语法说明(转)
  14. JAVA逻辑运算符
  15. std::list 源代码解析
  16. Redis in Docker on Linux Container
  17. linu下C语言之BMP图片操作编程(上)
  18. sqlalchemy 使用
  19. Google开源软负载seesaw
  20. Django 学习第六天——Django模型基础第一节

热门文章

  1. 原生H5页面模拟APP左侧滑动删除效果
  2. 手动安装MySQL8.0
  3. Read-Copy Update Implementation For Non-Cache-Coherent Systems
  4. vuex的简单例子和vue model组件
  5. OpenCV绘制朱利亚(Julia)集合图形
  6. 理解 iOS 和 macOS 的内存管理
  7. 转载:Adb远程连接Android系统(通过网络使用ADB(Connect to android with wifi))
  8. 本机Ajax异步通信
  9. python 判断一个数为?
  10. 扪心自问,强大的UI框架,给我们带来了什么?(作者因此写了一个GuiLite)