• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# asp.net 抓取需要登录的网页内容 抓取asp.net登录验证的网站

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 private void btnASPNET_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> postParams = new Dictionary<string, string>();
            postParams.Add("txtLoginId", "www.cnuunet.com");
            postParams.Add("txtPassword", "www.cnuunet.com");
            postParams.Add("btnLogin", "Sign in");
            textBox1.Text = GetAspNetCodeResponseDataFromWebSite(postParams, "http://www.cnuunet.com/login.aspx", "http://www.cnuunet.com/ProductList.aspx");
        }

        /// <summary>
        /// ASP.net页面登录方式 通过post密码方式读取网页内容
        /// 在本页面(login.aspx)的.cs文件内验证用户名和密码。
        /// Asp.net验证需要记住本次页面加载的__VIEWSTATE和__EVENTVALIDATION信息,并且需要button按钮的ID和Text信息
        /// </summary>
        /// <param name="postParams">用户名(用户名文本框的ID和内容)、密码(密码文本框的ID和内容)、summit按钮(button按钮的ID和Text)</param>
        /// <param name="getViewStateAndEventValidationLoginUrl">需要验证登陆信息的url</param>
        /// <param name="getDataUrl">需要抓取数据的网页url</param>
        /// <returns>抓取页面返回的html信息</returns>
        private string GetAspNetCodeResponseDataFromWebSite(Dictionary<string, string> postParams, string getViewStateAndEventValidationLoginUrl, string getDataUrl)
        {
            if (postParams == null || postParams.Keys.Count!=3)
            {
                string errorMessage = "参数中需要包含如下3个信息,缺一不可。用户名(用户名文本框的ID和内容)、密码(密码文本框的ID和内容)、summit按钮(button按钮的ID和Text)";
                MessageBox.Show(errorMessage);
                return errorMessage ;
            }

            try
            {
                CookieContainer cookieContainer = new CookieContainer();

                ///////////////////////////////////////////////////
                // 1.打开 MyLogin.aspx 页面,获得 GetVeiwState & EventValidation
                ///////////////////////////////////////////////////                
                // 设置打开页面的参数
                HttpWebRequest request = WebRequest.Create(getViewStateAndEventValidationLoginUrl) as HttpWebRequest;
                request.Method = "GET";
                request.KeepAlive = false;

                // 接收返回的页面
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                System.IO.Stream responseStream = response.GetResponseStream();
                System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
                string srcString = reader.ReadToEnd();

                // 获取页面的 VeiwState,分析返回的页面,解析出__VIEWSTATE的值          
                string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
                int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
                int j = srcString.IndexOf("\"", i);
                string viewState = srcString.Substring(i, j - i);

                // 获取页面的 EventValidation,分析返回的页面,解析出__VIEWSTATE的值                   
                string eventValidationFlag = "id=\"__EVENTVALIDATION\" value=\"";
                i = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length;
                j = srcString.IndexOf("\"", i);
                string eventValidation = srcString.Substring(i, j - i);

                ///////////////////////////////////////////////////
                // 2.自动填充并提交 Login.aspx 页面,提交Login.aspx页面,来保存Cookie
                ///////////////////////////////////////////////////

                // 将文本转换成 URL 编码字符串
                viewState = System.Web.HttpUtility.UrlEncode(viewState);
                eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
                
                // 要提交的字符串数据。格式形如:user=uesr1&password=123
                string postString = "";
                foreach (KeyValuePair<string, string> de in postParams)
                {
                    //把提交按钮中的中文字符转换成url格式,以防中文或空格等信息
                    postString += System.Web.HttpUtility.UrlEncode(de.Key.ToString()) + "=" + System.Web.HttpUtility.UrlEncode(de.Value.ToString()) + "&";
                }
                postString += string.Format("__VIEWSTATE={0}&__EVENTVALIDATION={1}", viewState, eventValidation);
                
                // 将提交的字符串数据转换成字节数组
                byte[] postData = Encoding.ASCII.GetBytes(postString);

                // 设置提交的相关参数
                request = WebRequest.Create(getViewStateAndEventValidationLoginUrl) as HttpWebRequest;
                request.Method = "POST";
                request.KeepAlive = false;
                request.ContentType = "application/x-www-form-urlencoded";
                request.CookieContainer = cookieContainer;
                request.ContentLength = postData.Length;

                // 提交请求数据
                System.IO.Stream outputStream = request.GetRequestStream();
                outputStream.Write(postData, 0, postData.Length);
                outputStream.Close();

                // 接收返回的页面
                response = request.GetResponse() as HttpWebResponse;
                responseStream = response.GetResponseStream();
                reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("GB2312"));
                srcString = reader.ReadToEnd();

                ///////////////////////////////////////////////////
                // 3.打开需要抓取数据的页面
                ///////////////////////////////////////////////////
                // 设置打开页面的参数
                request = WebRequest.Create(getDataUrl) as HttpWebRequest;
                request.Method = "GET";
                request.KeepAlive = false;
                request.CookieContainer = cookieContainer;

                // 接收返回的页面
                response = request.GetResponse() as HttpWebResponse;
                responseStream = response.GetResponseStream();
                reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
                srcString = reader.ReadToEnd();
                return srcString;
                ///////////////////////////////////////////////////
                // 4.分析返回的页面
                ///////////////////////////////////////////////////
                // ...... ......
            }
            catch (WebException we)
            {
                string msg = we.Message;
                return msg;
            }  
        }
    


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
扩展无极限:工厂设计模式与C#扩展方法的结合使用发布时间:2022-07-13
下一篇:
C#语言学习之旅(1):C#基础发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap