主要是通过java实现3GQQ的登陆,抓取好友信息(昵称,QQ号等等),以及获取聊天信息等等。
代码的灵感来自 @水之子哈哈 分享的一个php登陆3GQQ的代码。其实3GQQ的登陆以及抓取好友很简单,只要成功登陆后拿到SID就可以为所欲为了。代码写的很粗糙,主要是为了功能实现,还有很多可以优化的地方,把这个放出来,大家可以随意指点,欢迎吐槽。
整块代码的逻辑不算复杂,因为3GQQ的流程很简单。大家使用Chrome 或者 firefox打开 wap.3g.qq.com ,点击QQ打开登陆页面,然后右键查看网页源码就知道回事。在程序中使用的到,查看好友分组信息,根据分组获取该分组下的QQ好友等等。都是通过查看网页源码实现的。
这篇只是讲解登陆,下篇开始实现发送消息和获取消息。
具体的WebUtils的实现,请移步 http://url.cn/65b9am (PS:请问怎么复制自己的文章地址连接啊。。。)
第一步:
登陆3GQQ,拿到SID。
/**
* QQ登陆
*/
public static String login(String qq,String password){
HashMap<String, String> params=new HashMap<String, String>();
params.put("login_url", "http://pt.3g.qq.com/s?aid=nLogin");
params.put("sidtype", "1");
params.put("loginTitle", "手机腾讯网");
params.put("bid", "0");
params.put("qq", qq);
params.put("pwd", password);
params.put("loginType", "1");
try {
String response=WebUtils.doPost(QQ_LOGIN_URL, params, 0, 0);
int sidIndex=response.indexOf("sid");
SID=response.substring(sidIndex+4, sidIndex+28);
} catch (IOException e) {
e.printStackTrace();
}
return SID;
}
第二步,根据SID获取所有的分组信息,包括分组名,分组序号,该分组的URL链接等等。
/**
* 获取所有分组信息
*/
public static List<Group> getFrendGroup(String sid){
List<Group> groupList=null;
try {
String response=WebUtils.doGet("http://q16.3g.qq.com/g/s?sid="+sid+"&aid=nqqGroup");
Pattern pattern = Pattern.compile("(?<=border=\"0\"/>).+?(?=<span class=\"no\">)");
Matcher matcher=pattern.matcher(response);
groupList=new ArrayList<Group>();
Group group=null;
int i=-1;
while(matcher.find()){
i++;
group=new Group();
group.setGroupUrl("http://q32.3g.qq.com/g/s?sid="+sid+"&aid=nqqGrpF&name="+matcher.group()+"&id="+i+"&gindex="+i+"&pid=1");
group.setGroupIndex(i);
group.setGroupName(matcher.group());
groupList.add(group);
}
} catch (IOException e) {
e.printStackTrace();
}
return groupList;
}
第三步、根据获取到的分组信息抓取所有的好友QQ号,我这边是单线程实现的,大家可以考虑使用多线程的方式去分组获取。
/**
* 根据好友分组的列表信息获取所有信息
* @param groupInfoList
* @return
*/
public static List<String> getFriendsFromGroup(List<Group> groupInfoList) {
List<String> friendList=null;
if(groupInfoList!=null&&groupInfoList.size()!=0){
friendList=new ArrayList<String>();
for(Group group:groupInfoList){
String response;
try {
response = WebUtils.doGet(group.getGroupUrl());
} catch (IOException e) {
e.printStackTrace();
}
Pattern pattern = Pattern.compile("(?<=&u=).+?(?=&)");
Matcher matcher=null;
int hasNext=-1;
int pid=0;
do{
pid=pid+1;
response=QQClient.getFrindsByGroupUrl(getGroupUrl(group.getGroupUrl(),pid));
hasNext=response.indexOf("下页");
matcher=pattern.matcher(response);
while(matcher.find()){
String firendQQ=matcher.group();
friendList.add(firendQQ);
}
}while(hasNext!=-1);
}
}
return friendList;
}