HttpWebRequest webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
StreamReader responseReader = new StreamReader(
webRequest.GetResponse().GetResponseStream()
);
string responseData = responseReader.ReadToEnd();
responseReader.Close();
// extract the viewstate value and build out POST data
string viewState = ExtractViewState(responseData);
string postData =
String.Format(
"__VIEWSTATE={0}&UserLogin1:S_tbloginnamee={1}&UserLogin1:S_tbpassword={2}&UserLogin1:loginradio={3}&UserLogin1:S_btconfirm={4}",
viewState, "敏泰", "123456", "S_rbperson", "确 认"
);
// have a cookie container ready to receive the forms auth cookie
CookieContainer cookies = new CookieContainer();
CookieContainer reqcookies = new CookieContainer();
// now post to the login form
webRequest = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
webRequest.CookieContainer = cookies;
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();
// we don't need the contents of the response, just the cookie it issues
webRequest.GetResponse().Close();
CookieCollection cookieheader = webRequest.CookieContainer.GetCookies(new Uri(LOGIN_URL));
// now we can send out cookie along with a request for the protected page
webRequest = WebRequest.Create(SECRET_PAGE_URL) as HttpWebRequest;
webRequest.CookieContainer = cookies;
webRequest.Referer = SECRET_PAGE_URL;
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
webRequest.ContentType = "application/x-www-form-urlencoded";
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream(),Encoding.GetEncoding("gb2312"));
cookieheader = webRequest.CookieContainer.GetCookies(new Uri(SECRET_PAGE_URL));
// and read the response
responseData = responseReader.ReadToEnd();
responseReader.Close();