使用C#的HttpWebRequest模拟登陆访问人人网(转)
无论使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路:
第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest;
第二 模拟POST或者GET方式提交的数据;
第三 模拟请求的头;
第四 提交请求并获得响应,及对响应做我们所需要的处理。
这里我们以人人网的登录为例,将涉及到POST以及GET两种请求方式。
在之前的文章《免费网页抓包工具,火狐插件FireBug的抓包使用教程》中我们知道,登陆人人网的时候,一共做了一个POST请求以及两个GET请求,如下图:

观察这三个请求的详细信息,不难看出第一个GET请求的地址可以由POST的响应得到,而第二个GET请求的地址又由第一个GET的响应得到。
先来模拟第一个POST请求
- HttpWebRequest request = null;
- HttpWebResponse response = null;
- string gethost = string.Empty;
- CookieContainer cc = new CookieContainer(); // 若要从远程调用中获取COOKIE一定要为request设定一个CookieContainer用来装载返回的cookies
- string Cookiesstr = string.Empty;
- try
- {
- //第一次POST请求
- string postdata = "email=" + UserName.Replace("@", "%40") + "&password=" + PassWord + "&origURL=" + HostUrl + "&domain=renren.com";//模拟请求数据,数据样式可以用FireBug插件得到。人人网POST数据时,用户名邮箱中的“@”变为“%40”,所以我们也要作此变化
- 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.Referer = "http://www.renren.com/Login.do?rf=r&domain=renren.com&origURL=" + HostUrl;
- 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;
- //取第一次GET跳转地址
- StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- string content = sr.ReadToEnd();
- response.Close();
- string[] substr = content.Split(new char[] { '"' });
- gethost = substr[1];
- }
- 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);
- //取再次跳转链接
- StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- string ss = sr.ReadToEnd();
- string[] substr = ss.Split(new char[] { '"' });
- gethost = substr[1];
- 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);
- request.Abort();
- response.Close();
- }
- catch (Exception)
- {
- //第二次GET出错
- }
GET与POST请求大同小异,这里便不再累述。三次请求结束,保存好你的cookie string,每次请求的时候都赋给请求的头部,你就处于登录状态了。
人人网的HttpWebRequest登陆模拟很简单,但是POST及GET涉及到了,是个不错的案例。
当然,在.net想做自动访问的操作还可以使用WebBrowser控件,而且还能够和HttpWebRequest共用cookie,抛砖引玉一下不在本篇文章的讨论范围。
使用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 ...
随机推荐
- Kafka Broker | 命令行选项和过程
1.目标 在这个Apache Kafka教程中,我们将学习Kafka Broker.Kafka Broker管理主题中的消息存储.如果Apache Kafka有多个代理,那就是我们所说的Kafka集群 ...
- 统一封装json返回结果
import org.codehaus.jackson.annotate.JsonIgnore; import org.codehaus.jackson.map.annotate.JsonSerial ...
- python基础 — time库
时间获取-------time() ctime() gmtime() 时间格式化-------strftime() strptime() 程序计时-------sleep() perf_count ...
- tft_LCD一些引脚极性设置方法:vsync, hsync, VBLANK
转载:https://blog.csdn.net/u014170207/article/details/52662988/ 在RGB模式中,LCD数据的扫描是以行为单位的.HSYNC是水平同步信号.P ...
- crontab 定时删除
/60 * * * /bin/find /usr/local/****/****/****/****/****.log.2019* -exec rm -f {} ; >/dev/null 2&g ...
- webpack4打包报错ERROR in multi ./src/main.js dist/bundle.js
webpack打包测试: 上边将mode01.js模块及main.js主文件编写完成,下边使用webpack对这些js文件进行打包 1.进入程序目录,执行webpack main.js build.j ...
- 在 WPF 程序中应用 Windows 10 真?亚克力效果
原文:在 WPF 程序中应用 Windows 10 真?亚克力效果 从 Windows 10 (1803) 开始,Win32 应用也可以有 API 来实现原生的亚克力效果了.不过相比于 UWP 来说, ...
- SpringCloud整合过程中jar依赖踩坑经验
今天在搭建SpringCloud Eureka过程中,一直在报pom依赖错误,排查问题总结如下经验. 1.SpringBoot整合SpringCloud两者版本是有严格约束的,详细见SpringBoo ...
- C# vb实现浮雕特效滤镜效果
在.net中,如何简单快捷地实现Photoshop滤镜组中的浮雕效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步 ...
- iOS学习之字符串(NSString)的截取、匹配、分隔
截取 NSString *str1 = @"this is zero"; 1.从第三个字符开始,截取长度为2的字符串.........注:空格算作一个字符 NSString *st ...