HttpWebRequest自动登录网站并获取网站内容(不包含验证码的网站)

简介:

可以使用 Visual Sniffer(百度搜索) 来捕捉提交的数据信息:
1. 访问你需要站外提交的页面,比如 CSDN 登陆页  http://www.csdn.net/member/UserLogin.aspx
2. 填写好需要的资料,比如用户名和密码,
3. 打开 Visual Sniffer, 点“开始拦截”
4. 在访问的页面中提交。
5. 等提交成功之后,在 Visual Sniffer 中“停止拦截”
6. 在 Visual Sniffer 的左侧栏的加号中依次点开,右边是它拦截到的内容:
ExpandedBlockStart.gif
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
ExpandedBlockStart.gif
        protected static string cookieHeader;
        private void Page_Load(object sender, System.EventArgs e)
ExpandedBlockStart.gif        {
            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
        }


ExpandedBlockStart.gif        /// <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)
ExpandedBlockStart.gif        {
            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;
        }


ExpandedBlockStart.gif        /// <summary>
        
/// 功能描述:在PostLogin成功登录后记录下Headers中的cookie,然后获取此网站上其他页面的内容
        
/// </summary>
        
/// <param name="strURL">获取网站的某页面的地址</param>
        
/// <param name="strReferer">引用的地址</param>
        
/// <returns>返回页面内容</returns>

        public static string GetPage(string strURL,string strReferer)
ExpandedBlockStart.gif        {
            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;
        }



本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/articles/1532591.html,如需转载请自行联系原作者

相关文章
|
11月前
|
安全 数据安全/隐私保护
浏览器无法保存密码,每次登录网页都要重新登录
浏览器无法保存密码,每次登录网页都要重新登录
浏览器无法保存密码,每次登录网页都要重新登录
|
Android开发
微信公众平台网页登录授权多次重定向跳转,导致code使用多次问题
微信公众平台网页登录授权多次重定向跳转,导致code使用多次问题
468 0
|
JSON 缓存 JavaScript
网页分享链接和网页授权获取用户的信息
最近做一个新项目接触到和微信网页授权有关的两方面的内容: 1. 分享链接, 自定义标题、描述、分享图片。 2. 网页授权获取用户的信息。
264 0
|
C++
直接用IE浏览网页 VS 在“我的电脑”里访问网页——保存cookie的问题。
     直接用IE浏览网页、在“我的电脑”里输入网址,这两种方法都可以访问网页,而且“我的电脑”里输入网址输入网址后(按回车),“我的电脑”就会变成IE的形式,表面上看这两种方式是没有什么却别的,但是,如果是一个OA,里面有window.open的形式打开的窗口,并且使用cookie的方式要验证用户是否登录,那么区别就显现出来了。
862 0
|
Web App开发 安全 .NET
网站被提示该网站内容被禁止访问
很多建站公司都在使用Kindeditor开源的图片上传系统,该上传系统是可视化的,采用的开发语言支持asp、aspx、php、jsp,几乎支持了所有的网站可以使用他们的上传系统,对浏览器的兼容以及手机端也是比较不错的,用户使用以及编辑上传方面得到了很多用户的喜欢。
2721 0
|
Web App开发 安全 索引
网站点击进去出现“该内容被禁止访问”的拦截处理
很多客户的网站被拦截并提示:“该内容被禁止访问”,大多数客户使用的都是阿里云的虚拟主机以及ECS服务器,最近发生这种问题的网站也越来越多,这几天刚解决完一个客户网站被阿里云:违规URL屏蔽访问处理通知并拦截,经过朋友介绍找到我们SINE安全公司。
5135 0
|
机器学习/深度学习 Web App开发 JavaScript
使用ApiPost测试接口时需要先登录怎么办?利用Cookie模拟登陆!
在后台在开发、调试接口时,常常会遇到需要登陆才能请求的接口。本文主要介绍利用ApiPost发送Cookie,使服务器识别已登录用户的Cookie。
|
数据采集 Python Web App开发
python爬虫之cookie方式自动登录巴比特网
最近打算抓取公司内部PPT模板库中的PPT,发现被各种安全屏蔽。因为好久没写程序了,因此写几个例子回顾下基本的爬虫知识 目标网址巴比特的登录页面 http://8btc.
1734 0
|
PHP
thinkphp生成的验证码不显示问题解决
在调用验证码之前加上 ob_clean();不显示验证码的代码: public function verify(){                 $verify = new \Think\Verify();                 $verify->entry();      ...
1335 0