微信的模拟登录及获取好友列表

最近没事写了个微信模拟登录的代码,测试能够到今天2013年11月4日为止是能够登录的 html

登录是用的jsoup实现的,一个简单又强大的工具。不懂的能够@红薯站长去 java

Connection.Response response = Jsoup.connect("https://mp.weixin.qq.com/") json

                .userAgent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0")
                .method(Connection.Method.GET).timeout(0)
                .execute();


        response = Jsoup.connect("https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN").ignoreContentType(true)
                .userAgent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0")
                .referrer("https://mp.weixin.qq.com/")
                .data("username", username)
                .data("pwd", DigestUtils.md5Hex(password))
                .data("f", "json")
                .data("imgcode", "")
                .cookies(response.cookies())
                .method(Connection.Method.POST)
                .execute();

        System.out.println(response.body()); tomcat

        cookies=response.cookies();//保存,之后得用 微信

这里返回的结果里面ErrMsg里面的地址很重要,它就是返回的302状态要重定向的地址。最重要的是里面包含了一个token.因此要保存一下 cookie

TOKEN = getQueryParam(homePage).get("token");

getQueryParam方法我就不贴出来了,很是简单,你能够用如今的httpclient,jetty或者tomcat里面的类实现,也能够本身写一个字符串处理的,方法不少,可是目的只有一个,我想要获得token的值。由于之后的url都要加上它。 工具

登录以后固然是获取好友列表了,我把前面的cookie用一个变量cookies保存起来了。下面用一下 测试

List<WeixinUser> userList=new ArrayList<WeixinUser>();
        String FANS_URL = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&token="
                + TOKEN + "&lang=zh_CN&pagesize=10&pageidx=0&type=0&groupid=0";


        WebClient wc = new WebClient();
        wc.getBrowserVersion().setUserAgent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0");


        wc.addRequestHeader("Referer", "https://mp.weixin.qq.com/cgi-bin/settingpage?t=setting/index&action=index&token=" + TOKEN + "&lang=zh_CN");
        wc.getOptions().setJavaScriptEnabled(true);
        wc.getOptions().setCssEnabled(true);
        wc.setJavaScriptEngine(new JavaScriptEngine(wc));
        wc.getOptions().setThrowExceptionOnScriptError(false);
        wc.getOptions().setTimeout(60000);
        CookieManager cm = new CookieManager();
        cm.setCookiesEnabled(true);
        for (Map.Entry<String, String> cookie : this.cookies.entrySet()) {
            Cookie c = new Cookie("mp.weixin.qq.com", cookie.getKey(), cookie.getValue());
            cm.addCookie(c);
        }
        wc.setCookieManager(cm);
        HtmlPage page = wc.getPage(FANS_URL);
        Document document = Jsoup.parse(page.asXml());
        Element elem = document.getElementById("userGroups");
        Elements items = elem.getElementsByAttributeValueContaining("class", "user_info");
        for (Element item : items) {
            WeixinUser user = new WeixinUser();
            Element child=item.select("a[data-fakeid][class*=remark_name]").first();
            user.setFakeId(child.attr("data-fakeid"));
            user.setRemarkName(child.text());
            userList.add(user);
        }
        return userList;
this


为何这里又用htmlunit了呢?由于腾讯实在是太贱了,用户列表那里经过js输出的,因此你得让它的js运行,而后拿结果。OK 到了这里剩下的事你想要作什么就看你的了。
url

还有一点,不要用低版本的jdk 至少是1.6_45的,由于这个SSLFactory的实现有了些变化,1.6.10的就不行。sun包里的东西 常常换,难怪sun对外声名不要在本身的程序中直接引用sun.xxxx的包,由于你不知道你用的那个类在下个版本中是什么样,甚至还有没有都不肯定。

相关文章
相关标签/搜索