刷人人的代码

    之前也说到了,想要把刷人人的代码上传的,结果因为人人的验证机制,多次登录之后,需要输入验证码,然后这个解析验证码就超出我能力范围了,所以每天只能测试20次左右,加上最近在帮科协写一个网站加客户端,所以一直没有空,所以拖到现在,也参考了其他人的代码,比如说chenchun的查找requestToken的代码,不过需要说的是,有些网上的代码年代很久远,人人的POST Action已经改变了,所以那些代码已经没有什么参考价值了,希望大家不要被误导。

    用法基本上在main函数里,可以尝试使用下。

    还有注意的一点,为了排版好看,所有的”<>”符号全部使用”《》”,到时候就替换掉就行了。有什么问题,留言吧。

下面就是代码:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
  
/** 
 * 操作人人网 比如登陆,发状态,模拟你访问任何人主页等等 
 * @author Mike 
 * @created 2013-2-21 
 * @version 1.0 
 */  
public class MainRenren {  
  
    // 人人网账号  
    private String userName = "";  
    // 人人网密码  
    private String password = "";  
  
    // 实时更改的字串  
    private String requestToken; 
    private String _rtf;
    private static String redirectURL = "http://www.renren.com/home";  
    private static String renRenLoginURL = "http://www.renren.com/PLogin.do";  
    // The HttpClient is used in one session  
    private HttpResponse response;  
    private DefaultHttpClient httpclient = null;  
  
    public MainRenren(String userName, String password) {  
        this.userName = userName;  
        this.password = password;  
    }  
  
    /** 
     * 登陆 
     * @return 是否成功 
     */  
    public boolean login() {  
        if (httpclient != null) {  
            System.out.println("检测是否登录");  
            return true;  
        }  
        httpclient = new DefaultHttpClient();  
        HttpPost httpost = new HttpPost(renRenLoginURL);  
        // All the parameters post to the web site  
        List<NameValuePair> nvps = new ArrayList《namevaluepair》();  
        nvps.add(new BasicNameValuePair("origURL", redirectURL));  
        nvps.add(new BasicNameValuePair("domain", "renren.com"));  
        nvps.add(new BasicNameValuePair("autoLogin", "true"));  
        nvps.add(new BasicNameValuePair("key_id", "1"));  
        nvps.add(new BasicNameValuePair("email", userName));  
        nvps.add(new BasicNameValuePair("password", password));  
        try {  
            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));  
            response = httpclient.execute(httpost);  
        } catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        } finally {  
            httpost.abort();  
        }  
        String redirectLocation = getRedirectLocation();  
        if (redirectLocation != null) {  
            requestToken = getRequestToken(getText(redirectLocation)); 
            _rtf = getRTF_Code(getText(redirectLocation));
        }  
        return true;  
    }  
  
    public String getRedirectLocation() {  
        Header locationHeader = response.getFirstHeader("Location");  
        if (locationHeader == null) {  
            return null;  
        }  
        return locationHeader.getValue();  
    }  
      
    public String getText(String redirectLocation) {  
        HttpGet httpget = new HttpGet(redirectLocation);  
        // Create a response handler  
        ResponseHandler《string》 responseHandler = new BasicResponseHandler();  
        String responseBody = "";  
        try {  
            responseBody = httpclient.execute(httpget, responseHandler);  
        } catch (Exception e) {  
            e.printStackTrace();  
            responseBody = null;  
        } finally {  
            httpget.abort();  
        }  
        return responseBody;  
    }  
  
    /** 
     * 更新本人状态 
     * @param _status更新的状态
     * @return 是否成功
     */  
    public boolean update(String _status) {  
        if (login()) {  
            HttpPost post = new HttpPost("http://shell.renren.com/480397089/status");  
            List《namevaluepair》 cp = new ArrayList《namevaluepair》();  
            cp.add(new BasicNameValuePair("content", _status));  
            cp.add(new BasicNameValuePair("channel", "renren"));  
            cp.add(new BasicNameValuePair("hostid", "480397089"));  
            cp.add(new BasicNameValuePair("requestToken", requestToken));  
            cp.add(new BasicNameValuePair("_rtk", _rtf));
            try {  
                post.setEntity(new UrlEncodedFormEntity(cp, HTTP.UTF_8));  
                response = httpclient.execute(post);  
                System.out.println(response.getStatusLine());  
            } catch (Exception e) {  
                e.printStackTrace();  
                return false;  
            } finally {  
                post.abort();  
            }  
            return true;  
        }  
        return false;  
    }  

  
    /** 
     * 访问 
     * @param id 访问好友的ID
     * @return 是否成功
     */  
    public boolean visit(int id) {  
  
        HttpPost post = new HttpPost("http://www.renren.com/" + id);  
        try {  
            response = httpclient.execute(post);  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        System.out.println("访问成功!id号:" + id);  
        post.abort();  
        return true;  
    }  
  
    /** 
     * 给好友留言,可以无限刷 
     * @param 好友ID ,留言内容message 
     * @return 是否成功
     */  
    public boolean lveMsgToAFriend(int id, String message) {  
        if (login()) {  
            HttpPost post = new HttpPost("http://gossip.renren.com/gossip.do");  
            List《namevaluepair》 cp = new ArrayList《namevaluepair》();  
            cp.add(new BasicNameValuePair("body", message));  
            cp.add(new BasicNameValuePair("curpage", ""));  
            cp.add(new BasicNameValuePair("from", "main"));  
            cp.add(new BasicNameValuePair("id", "" + id));  
            cp.add(new BasicNameValuePair("cc", "" + id));  
  
            cp.add(new BasicNameValuePair("ak", "f417ae2774f04cf03a5d5aafd9412c31")); //自己抓个包看看自己的,每个人都不一样
            cp.add(new BasicNameValuePair("cccc", ""));  
            cp.add(new BasicNameValuePair("tsc", ""));  
            cp.add(new BasicNameValuePair("headUrl", ""));  
            cp.add(new BasicNameValuePair("largeUrl", ""));  
            cp.add(new BasicNameValuePair("profilever", "2008"));  
            cp.add(new BasicNameValuePair("requestToken", requestToken));  
            cp.add(new BasicNameValuePair("only_to_me", "0"));  
            cp.add(new BasicNameValuePair("color", ""));  
            cp.add(new BasicNameValuePair("ref",  
                    "http://www.renren.com/profile.do&mode=&requestToken=" + requestToken));  
            try {  
                post.setEntity(new UrlEncodedFormEntity(cp, HTTP.UTF_8));  
                response = httpclient.execute(post);  
                System.out.println(response.getStatusLine());  
            } catch (Exception e) {  
                e.printStackTrace();  
                return false;  
            } finally {  
                post.abort();  
            }  
            return true;  
        }  
        return false;  
    }  
  
    /** 
     * 取到你所有的好友ID 
     * @return 好友id的list 
     */  
    public List《string》 getFriends() {  
        List《string》 friends = new ArrayList《string》();  
        if (login()) {  
            HttpGet get = new HttpGet("http://friend.renren.com/myfriendlistx.do");  
            ResponseHandler《string》 responseHandler = new BasicResponseHandler();  
            try {  
                String friendPage = httpclient.execute(get, responseHandler);  
                Pattern pattern = Pattern.compile("var friends=(.*);");  
                Matcher matcher = pattern.matcher(friendPage);  
                if (matcher.find()) {  
                    String str = matcher.group(1);  
                    Pattern p = Pattern.compile("\"id\":([1-9][0-9]{0,9})");  
                    Matcher m = p.matcher(str);  
                    while (m.find()) {  
                        friends.add(m.group(1));  
                    }  
                    return friends;  
                }  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
                get.abort();  
            }  
        }  
        return null;  
    }  
  
    /** 	
     * 得到renren的requestToken 
     * @author chenchun 
     * @created 2012-4-18 
     * @param homeHtml 
     * @return 
     */  
    public String getRequestToken(String _homeHtml) {  
        Pattern pattern = Pattern.compile("requesttoken=(-?[1-9][0-9]*)");  
        //System.out.println(homeHtml);
        Matcher m = pattern.matcher(_homeHtml);  
        if (m.find()) {  
            return m.group(1);  
        }  
        return null;  
    }  
  
    /**
     * 得到更新状态的_rtf
     * @param _homeHtml 主页的HTML源码
     * @author mikecoder
     * @created 2013-2-21
     * @return 一个_rtf的String
     */
    public String getRTF_Code(String _homeHtml) {
    	int rtf_index = _homeHtml.indexOf("get_check_x:'");
    	return _homeHtml.substring(rtf_index + 13, rtf_index + 20);
	}
    
    
    /**
     * @param args
     * @throws InterruptedException 这个错误你们自己看着办吧
     */
    public static void main(String[] args) throws InterruptedException {  
        MainRenren lw = new MainRenren("你的邮箱帐号", "你的密码");  
        lw.login();  
       	lw.update("test");
        lw.lveMsgToAFriend(你想留言的人的ID(int), "afcydtawftydfty");
        lw.visit(你想访问的人的ID(int));
    }  
}

Leave a Reply

Your email address will not be published. Required fields are marked *