首页  ·  知识 ·  云计算
HttpWebRequest自动登录网站并获取网站内容(不包含验证码的网站
hanguoji  博客园|  综合  编辑:德仔   图片来源:网络
可以使用 Visual Sniffer(百度搜索) 来捕捉提交的数据信息: 1. 访问你需要站外提交的页面,比如 CSDN 登陆页 htt
可以使用 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;
        }
本文作者:hanguoji 来源:博客园|
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读