public void Post2() { string url = "http://jwc.jxnu.edu.cn/Portal/LoginAccount.aspx?t=account"; //得到表单中所有要提交的元素的name和value。 List <string> values = WebHelper.GetElementContent(url, "input", "value"); List <string> names = WebHelper.GetElementContent(url, "input", "name"); //自动填充表单 Dictionary <string, string> postPair = new Dictionary <string, string>(); for (int i = 0, len = names.Count(); i < len; i++) { postPair.Add(names.ElementAtOrDefault(i), values.ElementAtOrDefault(i)); } //利用一个字典,将对应的name和value放进去。当然,要想登录,我们还是得需要用户名和密码: WebHelper.SetParams("_ctl0:cphContent:ddlUserType", "Student", postPair); WebHelper.SetParams("_ctl0:cphContent:txtUserNum", "1467004007", postPair); WebHelper.SetParams("_ctl0:cphContent:txtPassword", "cx763225207", postPair); WebHelper.SetParams("_ctl0:cphContent:btnLogin", "登录", postPair); WebHelper.UrlEncodeParams(postPair); CookieContainer cookies = new CookieContainer(); string postStr = ""; foreach (string key in postPair.Keys) { postStr += key + "=" + postPair[key] + "&"; } byte[] postData = Encoding.ASCII.GetBytes(postStr.Substring(0, postStr.Length - 1)); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.AllowAutoRedirect = false; request.ContentType = "application/x-www-form-urlencoded;charset=gbk"; request.CookieContainer = new CookieContainer(); request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"; request.ContentLength = postData.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(postData, 0, postData.Length); requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); string cookie = response.Headers.Get("Set-Cookie"); string resultPage = reader.ReadToEnd(); //cookie = System.Web.HttpUtility.UrlEncode(cookie); //Response.Write("登录测试HTML结果:<br />" + resultPage+"<br /> Cookie="+cookie); string LoginResult = "失败"; if (resultPage.Contains("Object moved to")) { LoginResult = "成功"; } logger.Debug("【江西师范大学教务在线模拟登录】登录测试HTML结果: 登录" + LoginResult + "\r\n Html内容为:" + resultPage + "\r\n Cookie=" + cookie); Response.Write("【江西师范大学教务在线模拟登录】登录测试HTML结果:登录" + LoginResult + "<br /> Html内容为:" + resultPage + "<br /> Cookie=" + cookie); //带着返回的Cookie跳转新页面 //string html = WebHelper.GetHtml("http://jwc.jxnu.edu.cn/Portal/index.aspx",WebHelper.GetCookieName(cookie), WebHelper.GetCookieValue(cookie)); string html = WebHelper.GetHtml("http://jwc.jxnu.edu.cn/Portal/index.aspx", "JwOAUserSettingNew", WebHelper.GetCookieValue(cookie)); reader.Close(); responseStream.Close(); Response.Write(html); }