Java登陸3GQQ以及獲取好友信息與好友聊天的簡單實現
主要是通過java實現3GQQ的登陸,抓取好友信息(昵稱,QQ號等等),以及獲取聊天信息等等。 其實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; }
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!