您的位置:首页 > 编程语言 > Java开发

人人网外挂的java源代码(自动登录人人网并访问其他人主页)

2011-04-28 14:06 661 查看
最近在学习java,并对以前有人使用的人人网刷人气外挂程序比较感兴趣,就开始了在网上查阅各种资料,准备写一段java代码,实现自动登录人人网并访问其他人主页的功能。

使用浏览器手动登录大致过程可以这样描述: 1、登录到人人网登录页面;2、输入用户名和密码,登录;3、登录成功将跳转到首页;4、访问其他人主页。
要想自己的来访用户增多,其中之一的方法就是大量的访问别人的个人主页,其结果就是人气增加。个人登录成功后,点击其他链接并跳转时,都将会有一个身份验证的过程,人人网服务器后台将设置响应的cookie并保存在客户端。
如果只是简单的登录而不需要身份验证的话,使用java标准api中的java.net.URL完全可以实现,而且代码可以写的很简洁。但如果使用标准api设置cookie是很繁琐的。可以使用httpcomponents-client-4.1.1-bin工具包(可以到apache官网上下载),使得编写代码简单很多。
下面来看具体代码(因为本人水平有限,所以代码写的不是很规范):

源码1.1:创建要登录的用户
//只包含用户名和密码两个成员变量
public class User {
private String userName = null;
private String password = null;

User() {

}

User(String userName, String password) {
this.userName = userName;
this.password = password;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String toString() {
return this.userName + ";" + this.password;
}

}

源码1.2:设置post方法发送信息
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class NameValue {
private List<NameValuePair> nvps = new ArrayList<NameValuePair>();

//设置要发送信息的键值对,通过查看人人网登陆页面可以知道有哪些
NameValue(User user) {
add("origURL","http://www.renren.com/Home");
add("domain","renren.com");
add("email",user.getUserName());
add("password",user.getPassword());
}

public List<NameValuePair> getNvps() {
return nvps;
}

public void setNvps(List<NameValuePair> nvps) {
this.nvps = nvps;
}

void add(String name,String value) {
nvps.add(new BasicNameValuePair(name,value));
}

}

源码1.3:用户登录
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Iterator;

import org.apache.http.HttpResponse;
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.protocol.HTTP;

public class UserLogin {
private HttpResponse response = null;
private DefaultHttpClient httpclient = new DefaultHttpClient();
private ResponseHandler<String> responseHandler = new BasicResponseHandler();

UserLogin(User user,HttpPost httppost) {
//根据用户信息设置键值对
NameValue nv = new NameValue(user);
try {
//使用post方法发送数据
httppost.setEntity(new UrlEncodedFormEntity(nv.getNvps(), HTTP.UTF_8));
response = httpclient.execute(httppost);
System.out.println("post成功!");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
System.out.println("设置失败!");
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
System.out.println("连接失败!");
e2.printStackTrace();
} finally {
//中值post数据
httppost.abort();
}

}

//post数据后会得到一个跳转的地址,即个人首页
String getMainText() {
String mainText = null;
//根据Location报头得到跳转的网址
String http = response.getFirstHeader("Location").getValue();
System.out.println("跳转到:" + http);

//获取跳转之后的网页内容
mainText = getText(http);
return mainText;
}

//获取http网址的网页内容
String getText(String http) {
HttpGet httpget = new HttpGet(http);
String responseBody = null;
try {
responseBody = httpclient.execute(httpget, responseHandler);
System.out.println("跳转成功!");
} catch (Exception e) {
e.printStackTrace();
responseBody = null;
} finally {
//中止get数据
httpget.abort();
}
return responseBody;
}

//访问所有id集合中的个人主页
HashSet<Integer> visitSet(HashSet<Integer> hs,String htm) {
HashSet<Integer> vs = new HashSet<Integer>();
Iterator<Integer> i = hs.iterator();
System.out.println("The Total Number is :" + hs.size());
int count = 0;
while(i.hasNext()) {
System.out.println("Visit the " + (++count) + ".st");
String str = getText(htm + i.next() + "");
vs.addAll(Id.catchByCard(str));
if(count>=100) break;
}

return vs;
}

//中断连接
void abortClient() {
httpclient.getConnectionManager().shutdown();
System.out.println("中断连接!");
}

}

源码1.4:提供各种获取id的静态方法
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//提供各种获取id的静态方法
public class Id {

static HashSet<Integer> catchByStr(String str) {
HashSet<Integer> idSet = new HashSet<Integer>();
idSet.addAll(catchByCard(str));
idSet.addAll(catchById1(str));
idSet.addAll(catchById2(str));
idSet.addAll(catchById3(str));
return idSet;
}

//从html文件中抓取id
static HashSet<Integer> catchHtmlFile(File f) {
HashSet<Integer> idSet = new HashSet<Integer>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
String str = null;
while((str=br.readLine())!=null) {
idSet.addAll(catchByStr(str));
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("文件不存在!");
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
System.out.println("文件不存在!");
}finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return idSet;
}

//从已经获取id的文本文件中读取id,文本格式要求严格
static HashSet<Integer> catchIdFile(File f) {
HashSet<Integer> idSet = new HashSet<Integer>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
String str = null;
while((str=br.readLine())!=null) {
if(str.matches("[0-9]{9}"))
idSet.add(Integer.parseInt(str));
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("文件不存在!");
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
System.out.println("文件不存在!");
}finally {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return idSet;

}

//首页中的一种id格式:namecard="..."
static HashSet<Integer> catchByCard(String str) {
HashSet<Integer> idSet = new HashSet<Integer>();
Pattern idp = Pattern.compile("namecard=/"[0-9]{9}/"");
Matcher m = idp.matcher(str);
while(m.find()){
String getStr = m.group();
//System.out.println(getStr);
String num = getStr.substring(getStr.indexOf("/"")+1, getStr.indexOf("/"")+10);
idSet.add(Integer.parseInt(num));
}

return idSet;

}

//首页中的一种id格式:id="..."
static HashSet<Integer> catchById1(String str) {
HashSet<Integer> idSet = new HashSet<Integer>();
Pattern idp = Pattern.compile("id=/"[0-9]{9}/"");
Matcher m = idp.matcher(str);
while(m.find()){
String getStr = m.group();
//System.out.println(getStr);
String num = getStr.substring(getStr.indexOf("/"")+1, getStr.indexOf("/"")+10);
idSet.add(Integer.parseInt(num));
}

return idSet;
}

//首页中的一种id格式:id=...
static HashSet<Integer> catchById2(String str) {
HashSet<Integer> idSet = new HashSet<Integer>();
Pattern idp = Pattern.compile("id=[0-9]{9}");
Matcher m = idp.matcher(str);
while(m.find()){
String getStr = m.group();
//System.out.println(getStr);
String num = getStr.substring(getStr.indexOf("=")+1, getStr.indexOf("=")+10);
idSet.add(Integer.parseInt(num));
}
return idSet;
}

//首页中的一种id格式:"id":...
static HashSet<Integer> catchById3(String str) {
HashSet<Integer> idSet = new HashSet<Integer>();
Pattern idp = Pattern.compile("/"id/":[0-9]{9}");
Matcher m = idp.matcher(str);
while(m.find()){
String getStr = m.group();
//System.out.println(getStr);
String num = getStr.substring(getStr.indexOf(":")+1, getStr.indexOf(":")+10);
idSet.add(Integer.parseInt(num));
}
return idSet;
}

//查看获取的用户id
@SuppressWarnings("rawtypes")
static void printSet(HashSet idSet) {
System.out.println("The Total Number :" + idSet.size() +"");
Iterator i = idSet.iterator();
int count = 0;
while(i.hasNext()) {
System.out.println((++count) + ":" + (Integer)i.next());
}
}

}

源码1.5:主程序
import java.util.HashSet;
import org.apache.http.client.methods.HttpPost;

public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//设置个人信息
User user = new User("输入你的邮箱地址","输入密码");

//设置接收post方法发送数据的目的网址
HttpPost httppost = new HttpPost("http://www.renren.com/PLogin.do");

//用户登陆
UserLogin ul = new UserLogin(user,httppost);

//获得首页内容
String str1 = ul.getMainText();

//跳转到用户的好友列表页面
String str2 = ul.getText("http://friend.renren.com/myfriendlistx.do");

//根据首页和好友列表抓取用户id
HashSet<Integer> hs = new HashSet<Integer>();
hs.addAll(Id.catchByStr(str1));
hs.addAll(Id.catchByStr(str2));

//访问其他用户的个人主页,并从主页中获取新的id
HashSet<Integer> mo = new HashSet<Integer>();

mo.addAll(ul.visitSet(hs,"http://www.renren.com/profile.do?id="));
//mo.addAll(ul.visitSet(hs,"http://www.renren.com/profile.do?id="));

System.out.println(mo.size());
//断开连接,用户退出
ul.abortClient();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: