WebRequest登录ASP.NET页面

 CookieContainer cookieContainer = new CookieContainer();

            // 设置打开页面的参数
            string URI = "http://xxx/logon.aspx";
            HttpWebRequest request = WebRequest.Create(URI) 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               
            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               
            string eventValidationFlag = "id=/"__EVENTVALIDATION/" value=/"";
            i = srcString.IndexOf(eventValidationFlag) + eventValidationFlag.Length;
            j = srcString.IndexOf("/"", i);
            string eventValidation = srcString.Substring(i, j - i);

         
            // 提交按钮的文本
            string submitButton = "登录";

            // 用户名和密码
            string userName = "***";
            string password = "***";

            // 将文本转换成 URL 编码字符串
            viewState = System.Web.HttpUtility.UrlEncode(viewState);
            eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
            submitButton = System.Web.HttpUtility.UrlEncode(submitButton);


            // 要提交的字符串数据。

            //Login1$UserName都是控件的NAME属性值
            string formatString =
                  "Login1$UserName={0}&Login1$Password={1}&Login1$LoginButton={2}&__VIEWSTATE={3}&__EVENTVALIDATION={4}";

            string postString =
                     string.Format(formatString, userName, password, submitButton, viewState, eventValidation);


            // 将提交的字符串数据转换成字节数组
            byte[] postData = Encoding.ASCII.GetBytes(postString);

            // 设置提交的相关参数
            request = WebRequest.Create(URI) 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.UTF8);
            srcString = reader.ReadToEnd();

 

            // 3. 打开 Default.aspx 页面
            // 设置打开页面的参数
            URI = "http://xxx/Default.aspx";
            request = WebRequest.Create(URI) 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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值