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

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

登陆是用的jsoup实现的,一个简单又强大的工具。不懂的可以@红薯站长去

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

                .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());

        cookies=response.cookies();//保存,以后得用

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

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;


为什么这里又用htmlunit了呢?因为腾讯实在是太贱了,用户列表那里通过js输出的,所以你得让它的js运行,然后拿结果。OK 到了这里剩下的事你想要做什么就看你的了。

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


转载于:https://my.oschina.net/u/997546/blog/174625

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值