[求助]Java获取QQ邮箱联系人,报错...
找个Java获取QQ邮箱联系人,提供源代码
package com.agileeagle.app.invite.importlist.email;import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.script.ScriptEngine;import javax.script.ScriptEngineManager;import org.apache.commons.httpclient.Cookie;import org.apache.commons.httpclient.NameValuePair;import org.htmlparser.Node;import org.htmlparser.NodeFilter;import org.htmlparser.Parser;import org.htmlparser.filters.AndFilter;import org.htmlparser.filters.HasAttributeFilter;import org.htmlparser.filters.TagNameFilter;import org.htmlparser.util.NodeList;import org.htmlparser.visitors.TagFindingVisitor;import com.agileeagle.app.invite.importlist.Contact;import com.agileeagle.app.invite.importlist.exception.AuthorMatchException;import com.agileeagle.app.invite.importlist.exception.ContactsException;import com.agileeagle.core.config.CharsetConfig;/** * QQ好友列表 * @author Administrator * */public class QQImporter extends EmailImporter { private String beforeLoginUrl = "https://mail.qq.com/cgi-bin/loginpage?"; private String loginImgUrl = "https://mail.qq.com/cgi-bin/getverifyimage?aid=23000101&f=html&ck=1&0.4268505945413007"; private final static String EXEC_JS = "var StartTime=(new Date()).valueOf();var PublicKey = \"CF87D7B4C864F4842F1D337491A48FFF54B73A17300E8E42FA365420393AC0346AE55D8AFAD975DFA175FAF0106CBA81AF1DDE4ACEC284DAC6ED9A0D8FEB1CC070733C58213EFFED46529C54CEA06D774E3CC7E073346AEBD6C66FC973F299EB74738E400B22B1E7CDC54E71AED059D228DFEB5B29C530FF341502AE56DDCFE9\";var RSA = new RSAKey();RSA.setPublic(PublicKey, \"10001\");"; private static String AUTHOR_JS = null; static{ try { StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(QQImporter.class.getClassLoader().getResourceAsStream("safeauth.js"),CharsetConfig.UTF_8)); String line = null; while((line=br.readLine())!=null){ sb.append(line).append("\n"); } AUTHOR_JS = sb.deleteCharAt(sb.length()-1).toString(); br.close(); } catch (Exception e) { e.printStackTrace(); } } private String urlHead = null; private String sid = null; private String targetUrl = null; // 好友联系人列表的URL private String contactsUrl = "addr_listall?grpid=1000&isqqgroup=1&sid="; /** * 构造函数 * * @param email * @param password */ public QQImporter(String email, String password) { super(email, password,"GBK"); } /** * 登录Gmail邮箱 * * @throws ContactsException */ public void doLogin() throws ContactsException { try { String content = doGet(beforeLoginUrl); ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript"); engine.eval(AUTHOR_JS); engine.eval(EXEC_JS); Object startTime = engine.get("StartTime"); String ts = getInputValue("name=\"ts\"", content); StringBuilder pp = new StringBuilder(); for(int i=0;i<password.length();i++){ pp.append("0"); } StringBuilder encrypt = new StringBuilder(); encrypt.append("var Res = RSA.encrypt('").append(password).append("'+'\\n'+'").append(ts).append("'+'\\n');if (Res){Res = hex2b64(Res);}"); engine.eval(encrypt.toString()); Object p = engine.get("Res"); String[] infos = email.split("@"); String username = infos[0]; String aliastype = "@"+infos[1]; if("@foxmail.com".equalsIgnoreCase(aliastype)){ aliastype="fox"; } NameValuePair[] params = new NameValuePair[] { new NameValuePair("sid", getInputValue("name=\"sid\"", content)), new NameValuePair("firstlogin",getInputValue("firstlogin", content)), new NameValuePair("ts",getInputValue("name=\"ts\"", content)), new NameValuePair("uin",username), new NameValuePair("aliastype",aliastype), new NameValuePair("f","html"), new NameValuePair("pp",pp.toString()), new NameValuePair("p",p.toString()), new NameValuePair("starttime",String.valueOf(startTime)), new NameValuePair("pp",password)}; client.getState().addCookies( new Cookie[] { new Cookie("qq.com","flv", "10.0", "/", null,false), new Cookie("mail.qq.com","exstype", "2", "/", null,false), new Cookie("mail.qq.com","showToday", "1", "/", null,false) }); String actionUrl = getFormUrl(content); doGet(loginImgUrl+new java.util.Random().nextInt(),""); content = doPost(actionUrl, params, beforeLoginUrl); int end = -1; if((end=content.indexOf("document.write"))!=-1){ int start = content.indexOf("<script>"); content = content.substring(start+"<script>".length(),end); engine.eval(content); targetUrl = engine.get("targetUrl").toString(); Pattern pattern =Pattern.compile("errtype=([^&]+)"); Matcher matcher = pattern.matcher(targetUrl); if(matcher.find()){ String code = matcher.group(1); if("1".equals(code)){ throw new AuthorMatchException("您输入的帐号密码不匹配."); }else{ throw new ContactsException("错误码为:"+code); } } pattern =Pattern.compile("sid=([^&]+)&r"); matcher = pattern.matcher(targetUrl); if(!matcher.find()){ throw new ContactsException("not found sid=([^&]+)&r"); } sid = matcher.group(1); urlHead = engine.get("urlHead").toString(); } this.removeCookies(new String[]{"tinfo"}); }catch (ContactsException e) { throw e; }catch (Exception e) { e.printStackTrace(); throw new ContactsException("QQMail protocol has changed", e); } } /** * 解析联系人列表 * * @throws ContactsException */ @SuppressWarnings("unchecked") public List<Contact> parseContacts() throws ContactsException { try { String content =doGet(urlHead+contactsUrl+sid,targetUrl); List<Contact> contacts = null; Parser parser = null; parser = new Parser(content); parser.setEncoding(encoding); NodeFilter filter = new AndFilter(new TagNameFilter("div"),new HasAttributeFilter("class", "M_l")); NodeList nodeList = parser.extractAllNodesThatMatch(filter); if(nodeList!=null && nodeList.size()>0){ contacts = new ArrayList<Contact>(); for (int i = 0; i <nodeList.size(); i++) { Node node = nodeList.elementAt(i); TagFindingVisitor p = new TagFindingVisitor(new String[]{"p"} ); node.accept(p); Node [] ns = p.getTags(0); if(ns.length>=2){ String username = ns[0].toPlainTextString(); String email =ns[1].toPlainTextString(); if(username!=null && email!=null){ username = username.replaceAll(" ", "").trim(); email = email.replaceAll(" ", "").trim(); if(!"".equals(username) && !"".equals(email)){ contacts.add(new Contact(username, email)); } } } } } return contacts; } catch (Exception e) { e.printStackTrace(); throw new ContactsException("QQMail protocol has changed", e); } }}