模拟验证的cookie
CookieContainer baseCookieContainer = new CookieContainer(); Cookie cookie2 = new Cookie("ASP.NET_SessionId", "sur1dby21qru00ol3bqcamqd"); cookie2.Domain = "106.37.230.254"; baseCookieContainer.Add(cookie2);
模拟POST提交数据
/// <summary> /// 返回LoginOncookie的值 /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns></returns> public Cookie GetLoginOnCookie(String username, String password, CookieContainer cookieContainer) { string pasturl = "http://106.37.230.254:81/"; string postData = "__VIEWSTATE=%2FwEPDwUKMTg0NDI4MDE5OGRkj8OrkkOlfYqdhxkeEVV4GsZ6FLw0IioIcl%2BnbwqoGbo%3D&__EVENTVALIDATION=%2FwEWBgKF6pivDAKl1bKzCQK1qbSRCwLoyMm8DwLi44eGDAKAv7D9Co04a1vpmJ%2FQuWDi2GFypJ8LBXRdxHsgxKaj%2FeIzgMJ6&txtUserName=5155555063&txtPassword=1955511&txtIMGCode=&BtnLogin=%E7%99%BB++%E5%BD%95&rcode="; Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebRequest request = null; Encoding encoding = System.Text.Encoding.GetEncoding("utf-8"); byte[] data = encoding.GetBytes(postData); // 准备请求... // 设置参数 request = WebRequest.Create(pasturl) as HttpWebRequest; request.Method = "POST"; request.AllowAutoRedirect = true; request.KeepAlive = true; request.Referer = "http://106.37.230.254:81/"; request.Headers.Add(HttpRequestHeader.AcceptCharset, "GBK,utf-8;q=0.7,*;q=0.3"); request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate"); request.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.5 Safari/534.7"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; request.Headers.Add("X-Requested-With", "XMLHttpRequest"); request.Headers.Add("DNT", "1"); request.CookieContainer = cookieContainer; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 Cookie LoginOnCookie = null; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.Cookies.Count == 0) { LoginOnCookie = null; } else { LoginOnCookie = response.Cookies[0]; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); } } return LoginOnCookie; }
模拟GET获取返回cookie,页面内容
public Cookie getCodeCookie(string url,CookieContainer cookieContainer) { HttpWebRequest request; // 设置参数 request = WebRequest.Create(url) as HttpWebRequest; request.Method = "GET"; request.AllowAutoRedirect = true; request.KeepAlive = true; request.Referer = "http://106.37.230.254:81/"; request.Headers.Add(HttpRequestHeader.AcceptCharset, "GBK,utf-8;q=0.7,*;q=0.3"); request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate"); request.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.5 Safari/534.7"; request.ContentType = "application/x-www-form-urlencoded"; request.Headers.Add("X-Requested-With", "XMLHttpRequest"); request.Headers.Add("DNT", "1"); request.CookieContainer = cookieContainer; Cookie CodeCookie = null; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { CodeCookie = response.Cookies[0]; GZipStream g = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress); StreamReader myreader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")); String responseText = myreader.ReadToEnd(); } return CodeCookie; } public String getStudentInfo(string url,CookieContainer cookieContainer) { HttpWebRequest request; // 设置参数 request = WebRequest.Create(url) as HttpWebRequest; request.Method = "GET"; request.AllowAutoRedirect = true; request.KeepAlive = true; request.Referer = "http://106.37.230.254:81/"; request.Headers.Add(HttpRequestHeader.AcceptCharset, "GBK,utf-8;q=0.7,*;q=0.3"); request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate"); request.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.5 Safari/534.7"; request.ContentType = "application/x-www-form-urlencoded"; request.Headers.Add("X-Requested-With", "XMLHttpRequest"); request.Headers.Add("DNT", "1"); request.CookieContainer = cookieContainer; String studentInfo=String.Empty; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { GZipStream g = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress); StreamReader myreader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")); studentInfo = myreader.ReadToEnd(); } return studentInfo; }
无奈网站出现验证码,切怎么输也不正确,致无法登陆 代码留存做个纪念!