日期:2014-05-18 浏览次数:20813 次
class Program { static string HostUrl = "http://www.renren.com/SysHome.do";//xiaonei主页 static string LoginUrl = "http://passport.renren.com/PLogin.do";//login主页 static CookieContainer cookecn = new CookieContainer(); static string email = "xxxxx"; static string pwd = "xxxxx"; static string fisrtgethost = string.Empty; static string Cookiesstr = string.Empty; static void Main(string[] args) { Console.Write("the first is post:\n"); string postdata = "email=" + email + "&password=" + pwd + "&origURL=" + HostUrl + "&domain=renren.com"; PostData(postdata); Console.Write("next host is:\n"); Console.Write(fisrtgethost); FirstGet(fisrtgethost, Cookiesstr); SecondGet(HostUrl); Console.ReadKey(); } static bool PostData(string postdata) { HttpWebRequest request = null; HttpWebResponse response = null; try { request = (HttpWebRequest)WebRequest.Create(LoginUrl); request.Method = "POST"; request.Referer = "http://www.renren.com/SysHome.do"; request.ContentType = "application/x-www-form-urlencoded"; request.AllowAutoRedirect = false; byte[] postdatabtyes = Encoding.UTF8.GetBytes(postdata); request.ContentLength = postdatabtyes.Length; request.CookieContainer = cookecn; request.KeepAlive = true; Stream requeststream = request.GetRequestStream(); requeststream.Write(postdatabtyes, 0, postdatabtyes.Length); requeststream.Close(); response = (HttpWebResponse)request.GetResponse(); response.Cookies = request.CookieContainer.GetCookies(request.RequestUri); CookieCollection cook = response.Cookies; string strcoook = request.CookieContainer.GetCookieHeader(request.RequestUri); Cookiesstr = strcoook; Console.Write(strcoook); Console.Write("\n"); for (int i = 0; i < cook.Count; i++) { Console.Write(cook[i]); Console.Write("\n"); } StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string content = sr.ReadToEnd(); response.Close(); string[] substr = content.Split(new char[] { '"' }); fisrtgethost = substr[1]; Console.Write("content is :\n"); Console.Write(content); } catch (System.Exception e) { Console.Write("error!"); return false; } return true; } static bool FirstGet(string url,string cookies) { Console.Write("\n*******************************************************************"); HttpWebRequest request = null; HttpWebResponse respone = null; CookieContainer cnter = new CookieContainer(); try { request = (HttpWebRequest)WebRequest.Create(