HttpWebRequest自动登录网站并获取网站内容(不包含验证码的网站)
可以使用 Visual Sniffer(百度搜索) 来捕捉提交的数据信息:
1. 访问你需要站外提交的页面,比如 CSDN 登陆页 http://www.csdn.net/member/UserLogin.aspx
2. 填写好需要的资料,比如用户名和密码,
3. 打开 Visual Sniffer, 点“开始拦截”
4. 在访问的页面中提交。
5. 等提交成功之后,在 Visual Sniffer 中“停止拦截”
6. 在 Visual Sniffer 的左侧栏的加号中依次点开,右边是它拦截到的内容:
POST http://www.csdn.net/member/UserLogin.aspx HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Referer: http://www.csdn.net/member/UserLogin.aspx
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Pragma: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1)
Host: www.csdn.net
Content-Length: 355
Proxy-Connection: Keep-Alive
Cookie: ASPSESSIONIDAAAATBQC=FMEGGCKDBKHAMMCGKPFDMBFG; ASP.NET_SessionId=lusprmnom05lr445tmteaf55; userid=699879
__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=dDwtMTcwMzgxNjQ2Mjs7bDxDU0ROVXNlckxvZ2luOmNiX1NhdmVTdGF0ZTtDU0ROVXNlckxvZ2luOkltYWdlX0xvZ2luOz4%2Btu1q2wmRZoAJTi9L73w1zBleylY%3D&CSDNUserLogin%3Atb_UserName=testusername&CSDNUserLogin%3Atb_Password=testpassword&CSDNUserLogin%3Atb_ExPwd=9232&from=&CSDNUserLogin%3AImage_Login.x=36&CSDNUserLogin%3AImage_Login.y=6
GET http://www.csdn.net/mycustompage.htm?aspxerrorpath=/member/UserLogin.aspx HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Referer: http://www.csdn.net/member/UserLogin.aspx
Accept-Language: zh-cn
UA-CPU: x86
Pragma: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1)
Host: www.csdn.net
Proxy-Connection: Keep-Alive
Cookie: ASPSESSIONIDAAAATBQC=FMEGGCKDBKHAMMCGKPFDMBFG; ASP.NET_SessionId=lusprmnom05lr445tmteaf55; userid=699879
以上为拦截内容,其中提交数据的参数部分(程序中的:strArgs)如:
__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=dDwtMTcwMzgxNjQ2Mjs7bDxDU0ROVXNlckxvZ2luOmNiX1NhdmVTdGF0ZTtDU0ROVXNlckxvZ2luOkltYWdlX0xvZ2luOz4%2Btu1q2wmRZoAJTi9L73w1zBleylY%3D&CSDNUserLogin%3Atb_UserName=testusername&CSDNUserLogin%3Atb_Password=testpassword&CSDNUserLogin%3Atb_ExPwd=9232
protected static string cookieHeader;
private void Page_Load(object sender, System.EventArgs e)
{
string strReContent = string.Empty;
//登录
strReContent = PostLogin("http://www.mystand.com.cn/login/submit.jsp提交的页面","提交的参数:userid=hgj0000&password=06045369","引用地址:http://www.mystand.com.cn/");
//asp.net登录传递的参数需注意
//strReContent = PostLogin("http://www.mystand.com.cn/login.aspx","__VIEWSTATE=dDwtNjkzMjUyNDczO3Q8O2w8aTwzPjs%2BO2w8dDxwPHA8bDxUZXh0Oz47bDxcZTs%2BPjs%2BOzs%2BOz4%2BOz6aX2dtqkJTK%2BKbNPsjd7Op%2Fl26Iw%3D%3D&txtUserName=hxf&txtPassword=hxf0000&btnEnter=%E7%99%BB%E5%BD%95","http://www.mystand.com.cn/login.aspx");
//获取页面
strReContent = GetPage("http://www.mystand.com.cn/company/getdata.jsp?code=","引用地址:http://www.mystand.com.cn/");
//strReContent = GetPage("http://www.mystand.com.cn/Modules/index.aspx","http://www.mystand.com.cn/login.aspx");
//可以对获得的内容进行处理:strReContent
}
/**//// <summary>
/// 功能描述:模拟登录页面,提交登录数据进行登录,并记录Header中的cookie
/// </summary>
/// <param name="strURL">登录数据提交的页面地址</param>
/// <param name="strArgs">用户登录数据</param>
/// <param name="strReferer">引用地址</param>
/// <returns>可以返回页面内容或不返回</returns>
public static string PostLogin(string strURL,string strArgs,string strReferer)
{
string strResult = "";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
myHttpWebRequest.AllowAutoRedirect = true;
myHttpWebRequest.KeepAlive = true;
myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
myHttpWebRequest.Referer = strReferer;
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.Method = "POST";
CookieCollection myCookies = null;
CookieContainer myCookieContainer = new CookieContainer();
myHttpWebRequest.CookieContainer = myCookieContainer;
Stream MyRequestStrearm = myHttpWebRequest.GetRequestStream();
StreamWriter MyStreamWriter = new StreamWriter(MyRequestStrearm,Encoding.ASCII);
//把数据写入HttpWebRequest的Request流
MyStreamWriter.Write(strArgs);
//关闭打开对象
MyStreamWriter.Close();
MyRequestStrearm.Close();
HttpWebResponse response = null;
System.IO.StreamReader sr = null;
response = (HttpWebResponse)myHttpWebRequest.GetResponse();
cookieHeader = myHttpWebRequest.CookieContainer.GetCookieHeader(new Uri(strURL));
HttpContext.Current.Application.Lock();
HttpContext.Current.Application["cookieHeader"] = cookieHeader;
HttpContext.Current.Application.UnLock();
myCookies = response.Cookies;
sr = new System.IO.StreamReader(response.GetResponseStream(),Encoding.GetEncoding("gb2312")); // //utf-8
strResult = sr.ReadToEnd();
return strResult;
}
/**//// <summary>
/// 功能描述:在PostLogin成功登录后记录下Headers中的cookie,然后获取此网站上其他页面的内容
/// </summary>
/// <param name="strURL">获取网站的某页面的地址</param>
/// <param name="strReferer">引用的地址</param>
/// <returns>返回页面内容</returns>
public static string GetPage(string strURL,string strReferer)
{
string strResult = "";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
myHttpWebRequest.ContentType = "text/html";
myHttpWebRequest.Method = "GET";
myHttpWebRequest.Referer = strReferer;
myHttpWebRequest.Headers.Add("cookie:"+ cookieHeader);
HttpWebResponse response = null;
System.IO.StreamReader sr = null;
response = (HttpWebResponse)myHttpWebRequest.GetResponse();
sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312")); // //utf-8
strResult = sr.ReadToEnd();
return strResult;
}
转载于:https://www.cnblogs.com/gxh973121/archive/2008/04/07/1139718.html
HttpWebRequest自动登录网站并获取网站内容(不包含验证码的网站)相关推荐
- 如何使用python自动登录路由器且获取页面内容
python代码非常之简单,但是功能相当强大,这个方法是我在试验登录路由器的时候发现的 import urllib print urllib.urlopen("http://admin:ad ...
- php微信自动登录,利用php怎么实现一个自动登录微信并获取昵称的功能
利用php怎么实现一个自动登录微信并获取昵称的功能 发布时间:2021-02-18 14:55:53 来源:亿速云 阅读:58 作者:Leah 这篇文章将为大家详细讲解有关利用php怎么实现一个自动登 ...
- 自动登录网易云获取近一周常听歌单
selenium滑块拖动验证 1.前言 2.环境 3.代码 谢谢你的浏览(End) 1.前言 实验网站:网易云(https://music.163.com/) 本次实验主要用于练习cookie的使用 ...
- java实现自动登录,并获取数据
为抓取 web的一些隐私数据,需要先登录,然后才能获取这些数据,用程序来实现,就需要实现自动登录,然后将登录信息保存在Cookie中,以便取得数据时,无须再次登录.以网易邮箱为例: 所需jar包:co ...
- 用Python如何自动登录路由器!获取信息!请勿用于非法用途!
转载自:计算机与网络安全 小编给大家推荐一个学习氛围超好的地方,Python学习交流裙:3零4零5零799!裙里都是学习软件开发的!所以看到了快点进来吧,不然就满了!里面资料都是大家贡献的,几百个G了 ...
- python爬虫实例——session自动登录并爬取相关内容
1.理解下 session (会话) 所谓的会话,你可以理解成我们用浏览器上网,到关闭浏览器的这一过程.session是会话过程中,服务器用来记录特定用户会话的信息. 比如今天双11,你淘宝网浏览了哪 ...
- 自动化测试 - 12306火车票网站自动登录工具
还记得2011年春运,12306火车票预订网站经常崩溃无法登录吗. 今天我们就开发一个12306网站自动登录软件. 帮助您轻松订票 Web的原理就是,浏览器发送一个Request给Web服务器,Web ...
- 自动化测试 (一) 12306火车票网站自动登录工具
还记得2011年春运,12306火车票预订网站经常崩溃无法登录吗. 今天我们就开发一个12306网站自动登录软件. 帮助您轻松订票 通过前两篇博客Fiddler教程和HTTP协议详解,我们了解了Web ...
- 自动化测试项目实战 ——12306火车票网站自动登录工具
还记得前几年春运,12306火车票预订网站经常崩溃无法登录吗. 今天我们就开发一个12306网站自动登录软件. 帮助您轻松订票 通过前两篇博客Fiddler教程和HTTP协议详解,我们了解了Web的原 ...
最新文章
- Android中的事件处理机制
- 大规模使用 Apache Kafka 的20个最佳实践
- hdu 3183 A Magic Lamp(给一个n位的数,从中删去m个数字,使得剩下的数字组成的数最小(顺序不能变),然后输出)...
- CS224n笔记二:word2vec
- MySQL 删除数据的最好的方式
- 前端实现拖动滑块完成验证
- 中英文敏感词过滤API推荐
- 未来架构:从服务化到云原生
- WAP 2.0 VS WEB 2.0
- 程序员该如何有效的找工作?
- 天河服务器维修,天河云服务器升级
- android蓝牙传输文件到mysql_蓝牙opp文件发送过程剖析
- 矛盾依旧脱欧协议过关难 欧盟认为英将延后脱欧
- class uesrfun.php,帝国cms-TAG高级管理插件
- 【STM32】GPIO工作原理(八种工作方式超详细分析,附电路图)
- 郑厂长系列故事——逃离迷宫(C语言)
- 精益生产的本质和应用案例
- Ubuntu Server 16.04安装,磁盘分区。
- 水果食用大全 -- 果品食疗 - 香蕉
- Arch设置开机自动连接wifi网络:
热门文章
- Matlab将数字数组转换为字符数组(用于标明点号)
- 共线方程(百度百科)
- 滑动窗口的最小值问题
- 【读书笔记0101】Beginning linux programming-3rd
- python爬去朋友圈_python爬虫24 | 搞事情了,用 Appium 爬取你的微信朋友圈。
- python遗传算法_基于Python的遗传算法特征约简(附代码)
- 多元函数梯度下降 java_机器学习知识点(五)梯度下降法Java实现
- android 技能标签功能_android专业技能总结.doc
- 抽象类中不能有private的成员_【java基础】-- java接口和抽象类的异同分析
- ado.net mysql 连接池_ADO.NET中SQL Server数据库连接池