日期:2014-05-17  浏览次数:20579 次

欲哭无泪的WebRequest.Create模拟登录session问题
代码如下,能够登陆login.aspx成功并且获取url2(该页面是有会话的) 的内容,但是
如果我点击url2页面里面的新闻链接,转到它的详细内容页面,就提示未登录了,为什么状态没保存,怎么保存呢?感觉就是登录成功后获取网页内容显示后,会话就消失

            Dictionary<string, string> postParams = new Dictionary<string, string>();
            postParams.Add("username", userno);
            postParams.Add("Password", baseMethod.GetPassword(userno, 2));
            postParams.Add("Button1", "Sign in");
            string url1 = "http://xxx/login.aspx?Login";
            string url2 = "http://xxx/newslist.aspx";
            string result = baseMethod.GetAspNetCodeResponseDataFromWebSite(postParams, url1, url2);
            System.Web.HttpContext.Current.Response.Write(result);

调用下面的方法
 public static 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)";
                return errorMessage;
            }
            try
            {
                CookieContainer cookieContainer = new CookieContainer();
                
                // 设置打开页面的参数
                HttpWebRequest request = WebRequest.Create(getViewStateAndEventValidationLoginUrl) as HttpWebRequest;
                //request.Method = "get";
                //request.KeepAlive = false;
                //cookieContainer = request.CookieContainer;

                // 接收返回的页面
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
               &nb