日期:2014-05-18  浏览次数:20813 次

c# 登陆人人网的问题
首先说明:我纯属是为了测试一下网站自动登陆。
小弟用c#测试自动登陆人人网,高手如果做过,就知道人人网现在登陆的时候,是经过了一个post和三个get才登陆到主页的 。现在问题是,post和第一个get测试结果正确,但是第二个get确出现错误。不晓得为什么,请指点……
C# code

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(