清风的blog 优然探索

.NET WebClient 保存Session,Cookies 例子

string err = "";
            string str = "";
           
            HttpClient hc = new HttpClient();
            string postString = "UserID1=admin&UserPwd1=admin&DrpUserType=1002";
            //str = hc.GetSrc("http://192.168.0.55/admin/skylogin.aspx" + postString, "utf-8", out err);


            str = hc.PostData("http://192.168.0.55/admin/skylogin.aspx", postString, "utf-8", "utf-8", out err);

            textBox1.Text = str;

            str = hc.GetSrc("http://192.168.0.55/admin/loglist/oprlist.aspx", "utf-8", out err);
            textBox2.Text = str;

            #region POST


            //Regex reg = new Regex(@"(?is)<input[^>]*?value=""([^""]*)""[^>]*>");
            Regex reg = new Regex(@"(?is)<input type=""hidden"" name=""__VIEWSTATE[^>]*?value=""([^""]*)""[^>]*>");
            string viewState="", eventValidation="";
            Match m = reg.Match(str);
            if (m.Success)
            {
                viewState = m.Groups[1].Value;
            }

            Regex reg1 = new Regex(@"(?is)<input type=""hidden"" name=""__EVENTVALIDATION[^>]*?value=""([^""]*)""[^>]*>");

            Match m1 = reg1.Match(str);
            if (m1.Success)
            {
                eventValidation = m1.Groups[1].Value;
            }

            viewState = System.Web.HttpUtility.UrlEncode(viewState);
            eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);

            #endregion

            postString = "__VIEWSTATE=" + viewState;
            postString += "&__EVENTVALIDATION=" + eventValidation;
            postString += "&drpSystemType=" + System.Web.HttpUtility.UrlEncode("System");

            postString += "&btnSearch=" + System.Web.HttpUtility.UrlEncode("确认搜索");
           
            str = hc.PostData("http://192.168.0.55/admin/loglist/oprlist.aspx", postString, "utf-8", "utf-8", out err);
            textBox3.Text = str;

2012年2月23日 | 发布:admin | 分类:技术文章 | 评论:0

发表留言: