《网页自动登录和提交POSt信息的核心》由会员分享,可在线阅读,更多相关《网页自动登录和提交POSt信息的核心(7页珍藏版)》请在金锄头文库上搜索。
1、网页自动登录和提交 POSt 信息的核心大升刷票 整理网页自动登录和提交 POST 信息的核心就是分析网页的源代码(HTML ),在 C#中,可以用来提取网页 HTML 的组件比较多,常用的用WebBrowser、WebClient、HttpWebRequest 这三个。以下就分别用这三种方法来实现:1、WebBrowser 是个迷你浏览器,其特点是 Post 时不用关心 Cookie、内置 JS 等问题WebBrowser 是 VS2005 新提供的组件(其实就是封装了 IE 接口),实现 POST 功能一般在 webBrowser 的 DocumentCompleted 中分析 Html
2、Document 来实现,代码如下:HtmlElement ClickBtn =null;if (e.Url.ToString().ToLower().IndexOf(http:/ 0) /登陆页面HtmlDocument doc = webBrowser1.Document;for (int i = 0; i http:/ = GetPage(http:/ = GetPage(http:/ / 功能描述:模拟登录页面,提交登录数据进行登录,并记录 Header 中的 cookie/ / 登录数据提交的页面地址/ 用户登录数据/ 引用地址/ 可以返回页面内容或不返回public static
3、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
4、, 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
5、= (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#实现的方法.