使用C#的HttpWebRequest模拟登陆访问人人网
使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路:
第一 启用一个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请求
- HttpWebRequest request = null;
- HttpWebResponse response = null;
- string gethost = string.Empty;
- CookieContainer cc = new CookieContainer();
- string Cookiesstr = string.Empty;
- try
- {
- //第一次POST请求
- 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,就可以直接复制了
- string LoginUrl="http://www.renren.com/PLogin.do";
- request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类
- request.Method = "POST";//数据提交方式为POST
- //模拟头
- request.ContentType = "application/x-www-form-urlencoded";
- byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
- request.ContentLength = postdatabytes.Length;
- request.AllowAutoRedirect = false;
- request.CookieContainer = cc;
- request.KeepAlive = true;
- //提交请求
- Stream stream;
- stream = request.GetRequestStream();
- stream.Write(postdatabytes, 0, postdatabytes.Length);
- stream.Close();
- //接收响应
- response = (HttpWebResponse)request.GetResponse();
- //保存返回cookie
- response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
- CookieCollection cook = response.Cookies;
- string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
- Cookiesstr = strcrook;
- //从返回的stream当中取第一次GET跳转地址: The URL has moved <a href="http://www.renren.com/home">here</a>
- StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- string content = sr.ReadToEnd();
- response.Close();
- string[] substr = content.Split(new char[] { '"' });
- gethost = substr[1]; //http://www.renren.com/home
- }
- catch (Exception)
- {
- //第一次POST出错;
- }
注释写的很详细了,在这就不再分析,也许有人对request = (HttpWebRequest)WebRequest.Create(LoginUrl)有疑问,可以去google一下HttpWebRequest和WebRequest的区别,简单来说WebRequest是一个抽象类,不能直接实例化,需要被继承,而HttpWebRequest继承自WebRequest。
再模拟第一个和第二个GET请求
- try
- {
- request = (HttpWebRequest)WebRequest.Create(gethost);
- request.Method = "GET";
- request.KeepAlive = true;
- request.Headers.Add("Cookie:" + Cookiesstr);
- request.CookieContainer = cc;
- request.AllowAutoRedirect = false;
- response = (HttpWebResponse)request.GetResponse();
- //设置cookie
- Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
- //取再次跳转链接 The URL has moved <a href="http://www.renren.com/1915651750">here</a>
- StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- string ss = sr.ReadToEnd();
- string[] substr = ss.Split(new char[] { '"' });
- gethost = substr[1]; //http://www.renren.com/1915651750
- request.Abort();
- sr.Close();
- response.Close();
- }
- catch (Exception)
- {
- //第一次GET出错
- }
- try
- {
- //第二次GET请求
- request = (HttpWebRequest)WebRequest.Create(gethost);
- request.Method = "GET";
- request.KeepAlive = true;
- request.Headers.Add("Cookie:" + Cookiesstr);
- request.CookieContainer = cc;
- request.AllowAutoRedirect = false;
- response = (HttpWebResponse)request.GetResponse();
- //设置cookie
- Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string ss = sr.ReadToEnd();
- webBrowser1.Navigate("about:blank");
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write(ss);
- request.Abort();
- response.Close();
- }
- catch (Exception)
- {
- //第二次GET出错
- }
GET与POST请求大同小异,这里便不再累述。三次请求结束,保存好你的cookie string,每次请求的时候都赋给请求的头部,你就处于登录状态了。
使用C#的HttpWebRequest模拟登陆访问人人网的更多相关文章
- 使用C#的HttpWebRequest模拟登陆访问人人网(转)
无论使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路:第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest:第二 模拟POST或者GET方式提交 ...
- c# 使用 HttpWebRequest模拟登陆
c# 使用 HttpWebRequest模拟登陆(附带验证码) 分类: C# .net2010-06-04 00:50 35647人阅读 评论(43) 收藏 举报 c#exceptionstreams ...
- c# 使用 HttpWebRequest模拟登陆(附带验证码)
在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等. 先说下流程 1.使用httpwebrequest先进入你要登录的 ...
- 转:使用C#的HttpWebRequest模拟登陆网站
这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话的CooKie 并根据该CooKie继续访问登录后的页面,获取后续访问的页面数据. ...
- 使用HttpWebRequest模拟登陆阿里巴巴(alibaba、httpwebrequest、login)
前言 其实老喜欢取经,偶尔也得分享下.关于阿里巴巴国际站的登陆,过程有点复杂但是算不上难.一不小心少个东西倒也挺麻烦的. 主要是看下请求类HttpClient基本请求封装使用,AliClient模拟浏 ...
- 使用C#的HttpWebRequest模拟登陆网站
很久没有写新的东西了,今天在工作中遇到的一个问题,感觉很有用,有种想记下来的冲动. 这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话 ...
- HttpWebRequest 模拟浏览器访问网站
最近抓网页时报错: 要么返回 The remote server returned an error: (442)要么返回: 非法访问,您的行为已被WAF系统记录! 想了想,就当是人家加了抓网页的东西 ...
- HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/3284481.html] PostLogin :登录,并保存Cookie 1 public st ...
- C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用
public static string GetCookie(string requestUrlString, Encoding encoding, ref CookieContainer cooki ...
随机推荐
- Enhancing network controls in mandatory access control computing environments
A Mandatory Access Control (MAC) aware firewall includes an extended rule set for MAC attributes, su ...
- C#之Linq、where()、FindAll()的区别
原地址 C#之Linq.where().FindAll()的区别 对于实现了IEnumerable接口的类.类型.集合可以使用Linq.Linq的扩展方法where().FindAll()来查询符合谓 ...
- springCloud你要了解的都在这(方向性)
Spring Cloud作为一套微服务治理的框架,几乎考虑到了微服务治理的方方面面,之前也写过一些关于Spring Cloud文章,主要偏重各组件的使用,本次分享主要解答这两个问题:Spring Cl ...
- 矿Java开发学习之旅------>Java排序算法经典的二分法插入排序
一.折半插入排序(二分插入排序) 将直接插入排序中寻找A[i]的插入位置的方法改为採用折半比較,就可以得到折半插入排序算法.在处理A[i]时,A[0]--A[i-1]已经按关键码值排好序.所谓折半比較 ...
- string操作
常用的功能测试: #! -*- coding:utf-8 -*- import string s = 'Yes! This is a string' print '原字符串:' + s print ' ...
- 局部QEventLoop帮助QWidget不消失(也就是有一个局部事件循环始终在运行,导致程序被卡住那里,但仍可以接受事件。说白了就是有一个while语句死活不肯退出,直到收到退出信号)
熟悉的陌生人 Qt 是事件驱动的,所以当你用Qt的时候,几乎时时刻刻和 QEventLoop 打交道.,只是你可能没有意识到: QCoreApplicaton::exec() QApplication ...
- 使用Photoshop切图的三种方式
PhotoShop切图的三种方式 1. 原始切图 (1)选择工具栏中的切片工具 (2)找到要切片的元素,在右侧的图层框中,使元素背景隐藏,然后用切片工具选择需要切片的元素 (3)导出为web常 ...
- mysql重置root密码,并设置可远程访问
linux系统: mysqld_safe --skip-grant-tables & mysql -u root use mysql UPDATE user SET host = '%' wh ...
- C# 操作XML文档 使用XmlDocument类方法
W3C制定了XML DOM标准.很多编程语言中多提供了支持W3C XML DOM标准的API.我在之前的文章中介绍过如何使用Javascript对XML文档进行加载与查询.在本文中,我来介绍一下.Ne ...
- INCORRECT PERMISSIONS ON /USR/LIB/PO1KIT-AGENT-HELPER-1(NEEDS TO BE SETUID ROOT)
INCORRECT PERMISSIONS ON /USR/LIB/PO1KIT-AGENT-HELPER-1(NEEDS TO BE SETUID ROOT) # sudo chmod +s /us ...