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

第一 启用一个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,每次请求的时候都赋给请求的头部,你就处于登录状态了。

使用C#的HttpWebRequest模拟登陆访问人人网的更多相关文章

  1. 使用C#的HttpWebRequest模拟登陆访问人人网(转)

    无论使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路:第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest:第二 模拟POST或者GET方式提交 ...

  2. c# 使用 HttpWebRequest模拟登陆

    c# 使用 HttpWebRequest模拟登陆(附带验证码) 分类: C# .net2010-06-04 00:50 35647人阅读 评论(43) 收藏 举报 c#exceptionstreams ...

  3. c# 使用 HttpWebRequest模拟登陆(附带验证码)

    在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等. 先说下流程 1.使用httpwebrequest先进入你要登录的 ...

  4. 转:使用C#的HttpWebRequest模拟登陆网站

    这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话的CooKie 并根据该CooKie继续访问登录后的页面,获取后续访问的页面数据. ...

  5. 使用HttpWebRequest模拟登陆阿里巴巴(alibaba、httpwebrequest、login)

    前言 其实老喜欢取经,偶尔也得分享下.关于阿里巴巴国际站的登陆,过程有点复杂但是算不上难.一不小心少个东西倒也挺麻烦的. 主要是看下请求类HttpClient基本请求封装使用,AliClient模拟浏 ...

  6. 使用C#的HttpWebRequest模拟登陆网站

    很久没有写新的东西了,今天在工作中遇到的一个问题,感觉很有用,有种想记下来的冲动. 这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话 ...

  7. HttpWebRequest 模拟浏览器访问网站

    最近抓网页时报错: 要么返回 The remote server returned an error: (442)要么返回: 非法访问,您的行为已被WAF系统记录! 想了想,就当是人家加了抓网页的东西 ...

  8. HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用

    [一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/3284481.html] PostLogin :登录,并保存Cookie 1 public st ...

  9. C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用

    public static string GetCookie(string requestUrlString, Encoding encoding, ref CookieContainer cooki ...

随机推荐

  1. sqoop 1.4.7 安装配置/连接测试

    环境: hadoop2.7.7 mysql 8 zk 3.4.10 hive 3 1.上传并解压tar包后进入conf目录 拷贝sqoop-env-template.sh并重命名为sqoop-env. ...

  2. Python 图像处理: 生成二维高斯分布蒙版

    在图像处理以及图像特效中,经常会用到一种成高斯分布的蒙版,蒙版可以用来做图像融合,将不同内容的两张图像结合蒙版,可以营造不同的艺术效果. I=M∗F+(1−M)∗B 这里I 表示合成后的图像,F 表示 ...

  3. Struts2——(7)拦截器组件

    AOP:面向切面编程(通过配置文件来指定作用到目标对象) OOP:面向对象编程 AOP具有很好的可插拔特性,很灵活. 可用于封装共通的业务处理,之后可以通过配置作用到Action组件上. 共通的业务处 ...

  4. 买不起360随声wifi怎么办?这些都不是问题

    只需轻松一步,点击开启共享 软件下载地址:http://download.csdn.net/detail/lxq_xsyu/6384265 如果身边没有数据线怎么办?? 使用方法: 1.用手机连接Wi ...

  5. 设置npm淘宝镜像

    npm config set registry https://registry.npm.taobao.org

  6. [Servlet]Servlet工作流程及注意事项

    Servlet工作过程 采用Servlet完成Web实际的工作流应用程序是通过Tomcatserver公布服务,client与server遵循的端部之间的相互作用Http议完毕的. 详细工作流程例如以 ...

  7. VS编译环境中TBB配置和C++中lambda表达式

    TBB(Thread Building Blocks),线程构建模块,是由Intel公司开发的并行编程开发工具,提供了对Windows,Linux和OSX平台的支持. TBB for Windows ...

  8. 左右RAC CRS 自己主动启动

    左右CRS自己主动重新启动实验 一.检验ASM [root@rac1 ~]# /etc/init.d/oracleasm status Checking if ASM is loaded: yes C ...

  9. 矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series

    poj 1575  Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...

  10. Windows下安装MySQL(解压版本)

    解压缩 将下载到的文件解压缩到自己喜欢的位置,例如我自己的位置是D:\Program Files\mysql-5.7.10-winx64 添加环境变量 右键计算机->属性->高级系统设置- ...