参考这个 实现站外登录博客园:http://www.cnblogs.com/anjou/archive/2007/10/15/923770.html#1067574
返回的login.aspx 页面 html内容显示的是登录状态,可打开浏览器还是未登录,请问这个是什么原因?相关代码
CookieContainer cookieContainer = new CookieContainer();
///////////////////////////////////////////////////
// 1.打开 MyLogin.aspx 页面,获得 GetVeiwState & EventValidation
///////////////////////////////////////////////////
// 设置打开页面的参数
string URI = "http://passport.cnblogs.com/login.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);
///////////////////////////////////////////////////
// 2.自动填充并提交 MyLogin.aspx 页面
///////////////////////////////////////////////////
// 提交按钮的文本
string submitButton = "登 录";
// 用户名和密码
string userName = "11";
string password = "22";
// 将文本转换成 URL 编码字符串
viewState = System.Web.HttpUtility.UrlEncode(viewState);
eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
submitButton = System.Web.HttpUtility.UrlEncode(submitButton);
// 要提交的字符串数据。格式形如:user=uesr1&password=123
string formatString = "tbUserName={0}&tbPassword={1}&btnLogin={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://passport.cnblogs.com/login.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();
外部登陆,你收到成功登陆的信息后应该存一下cookie吧?