资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
网页自动登录和提交 POSt 信息的核心大升刷票 www.ruheshuapiao.com 整理网页自动登录和提交 POST 信息的核心就是分析网页的源代码(HTML ),在 C#中,可以用来提取网页 HTML 的组件比较多,常用的用WebBrowser、WebClient、HttpWebRequest 这三个。以下就分别用这三种方法来实现:1、WebBrowser 是个迷你浏览器,其特点是 Post 时不用关心 Cookie、内置 JS 等问题WebBrowser 是 VS2005 新提供的组件(其实就是封装了 IE 接口),实现 POST 功能一般在 webBrowser 的 DocumentCompleted 中分析 HtmlDocument 来实现,代码如下:HtmlElement ClickBtn =null;if (e.Url.ToString().ToLower().IndexOf(http:/sandou.cnblogs.com/) 0) /登陆页面HtmlDocument doc = webBrowser1.Document;for (int i = 0; i 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/ / 功能描述:模拟登录页面,提交登录数据进行登录,并记录 Header 中的 cookie/ / 登录数据提交的页面地址/ 用户登录数据/ 引用地址/ 可以返回页面内容或不返回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, */ / 功能描述:在 PostLogin 成功登录后记录下 Headers 中的 cookie,然后获取此网站上其他页面的内容/ / 获取网站的某页面的地址/ 引用的地址 / 返回页面内容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-8strResult = sr.ReadToEnd();return strResult;技术应用网页自动登录(提交 Post 内容)的用途很多,如验证身份、程序升级、网络投票等,以下是用 C#实现的方法.
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号