其实网上已经有别的高手写的问题。。但是人人网在2012年改变了一些机制 所以导致老的方法不行了(比如POST时候发送的密码以前是明文现在是密文)
比如:http://www.cnblogs.com/hoholuo/archive/2011/12/17/2290873.html
我的代码:            HttpWebRequest request = null;
            HttpWebResponse response = null;
            string gethost = string.Empty;
            CookieContainer cc = new CookieContainer(); // 若要从远程调用中获取COOKIE一定要为request设定一个CookieContainer用来装载返回的cookies
            string Cookiesstr = string.Empty;
            try
            {
                //第一次POST请求  
                string postdata = "email=test%40131x.com&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&captcha_type=web_login&password=38f32f1db907a1463c3c1914c34abd8b196913b376474e40f41558f6d7e8503c&rkey=d0cf42c2d3d337f9e5d14083f2d52cb2" + "&domain=renren.com";//模拟请求数据,数据样式可以用FireBug插件得到。人人网POST数据时,用户名邮箱中的“@”变为“%40”,所以我们也要作此变化  
                string LoginUrl = " http://www.renren.com/SysHome.do";
                request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类  
                request.Method = "POST";//数据提交方式为POST  
                //模拟头  
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
                request.ContentLength = postdatabytes.Length;
                request.Referer = "http://www.renren.com/SysHome.do";  
                //request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                //提交请求  
                Stream stream;
                stream = request.GetRequestStream();
                stream.Write(postdatabytes, 0, postdatabytes.Length);
                stream.Close();
                //接收响应  
                response = (HttpWebResponse)request.GetResponse();
                //保存返回cookie  
                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
                CookieCollection cook = response.Cookies;
                string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
                Cookiesstr = strcrook;
                //取第一次GET跳转地址  
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string content = sr.ReadToEnd();
                response.Close();
                string[] substr = content.Split(new char[] { '"' });
                gethost = substr[1];
            }
            catch (Exception e)
            {            }我用firebug抓到的包是这样的:
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control no-cache
Connection keep-alive
Content-Length 233
Content-Type application/x-www-form-urlencoded; charset=UTF-8
Cookie anonymid=hcd7q9yz-rgomld; _r01_=1; ick_login=b9611e52-f4aa-4ccb-8214-ebf229ac6a3a; loginfrom=syshome; depovince=GW; jebecookies=0dbaea9f-de0c-4516-933f-acea762ede98|||||; JSESSIONID=abcgkCeDYuWZr_VhIwXXt; feedType=239564175_hot; first_login_flag=1
Host www.renren.com
Pragma no-cache
Referer http://www.renren.com/SysHome.do
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0
X-Requested-With XMLHttpRequest
还有post的data是
email=test%40131x.com&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&captcha_type=web_login&password=38f32f1db907a1463c3c1914c34abd8b196913b376474e40f41558f6d7e8503c&rkey=d0cf42c2d3d337f9e5d14083f2d52cb2但是每次运行到response = (HttpWebResponse)request.GetResponse();就出错 
错误信息是:
Message "The remote server returned an error: (405) Method Not Allowed." string
请问如何解决

解决方案 »

  1.   

    从(405) Method Not Allowed.的错误类型来看,应该是http://www.renren.com/SysHome.do已经不提供登录接口了,将string LoginUrl = "http://www.renren.com/SysHome.do";改成string LoginUrl = "http://www.renren.com/ajaxLogin/login?1=1";试试,不过密码已经加密(貌似是MD5 64位),还有rkey不知是否是动态的,想要模拟登录还是有点难度
      

  2.   


    我们尽量不在csdn宣传流氓行为。我个人更反感这类“E语言”宣传。