你的分享就是我们的动力 ---﹥

微信模拟登陆,主动推送图文消息

时间:2013-06-07 15:56来源:www.chengxuyuans.com 点击:

代码简介

由于问的人比较多,决定把代码放出来,代码还未整理,里面有解析数据可以用正则表达式替换也没做,有需要的可以自己在此基础上修改。

代码片段

public boolean msgSend(MsgForm form, MsgType type) {
        try {
            if (!this.isLogin) {
                this._login();
            }
            if (this.isLogin) {
                form.setToken(this.token);
                PostMethod post = new PostMethod(POST_MSG);
                post.setRequestHeader(USER_AGENT_H, USER_AGENT);
                post.setRequestHeader(REFERER_H, INDEX_URL);
                post.setRequestHeader("Cookie", this.cookiestr);
                NameValuePair[] params = null;
                Part[] parts = null;
                switch (type) {
                    case TEXT:
                 
                        parts = new Part[]{
                                new StringPart("content", form.getContent(),
                                        "UTF-8"),
                                new StringPart("type", form.getType()),
                                new StringPart("error", form.getError()),
                                new StringPart("needcomment", form.getNeedcomment()),
                                new StringPart("groupid", form.getGroupid()),
                                new StringPart("sex", form.getSex()),
                                new StringPart("country", form.getCountry()),
                                new StringPart("province", form.getProvince()),
                                new StringPart("city", form.getCity()),
                                new StringPart("token", form.getToken()),
                                new StringPart("ajax", form.getAjax()),
                                new StringPart("t", "ajax-response")};
                        break;
                    case IMAGE_TEXT:
                        parts = new Part[]{
                                new StringPart("content", form.getContent(),
                                        "UTF-8"),
                                new StringPart("type", form.getType()),
                                new StringPart("error", form.getError()),
                                new StringPart("needcomment", form.getNeedcomment()),
                                new StringPart("groupid", form.getGroupid()),
                                new StringPart("sex", form.getSex()),
                                new StringPart("country", form.getCountry()),
                                new StringPart("province", form.getProvince()),
                                new StringPart("city", form.getCity()),
                                new StringPart("token", form.getToken()),
                                new StringPart("ajax", form.getAjax()),
                                new StringPart("t", "ajax-response")};
                        break;
                    default:
                        parts = new Part[]{
                                new StringPart("content", form.getContent(),
                                        "UTF-8"),
                                new StringPart("type", form.getType()),
                                new StringPart("error", form.getError()),
                                new StringPart("needcomment", form.getNeedcomment()),
                                new StringPart("groupid", form.getGroupid()),
                                new StringPart("sex", form.getSex()),
                                new StringPart("country", form.getCountry()),
                                new StringPart("province", form.getProvince()),
                                new StringPart("city", form.getCity()),
                                new StringPart("token", form.getToken()),
                                new StringPart("ajax", form.getAjax()),
                                new StringPart("t", "ajax-response")};

                        break;
                }
                RequestEntity entity = new MultipartRequestEntity(parts,
                        post.getParams());
                post.setRequestEntity(entity);
                int status;
                status = client.executeMethod(post);
                if (status == HttpStatus.SC_OK) {
                    String text = post.getResponseBodyAsString();
                    try {
                        MsgJson ret = JSON.parseObject(text, MsgJson.class);
                        this.msgSendCode = ret.getRet();
                        switch (this.msgSendCode) {
                            case 0:
                                this.msgSendMsg = "发送成功";
                                return true;
                            case -2:
                                this.msgSendMsg = "参数错误,请仔细检查";
                                return false;
                            case 64004:
                                this.msgSendMsg = "今天的群发数量已到,无法群发";
                                return false;
                            case -20000:
                                this.msgSendMsg = "请求被禁止,请仔细检查token是否合法";
                                return false;
                            default:
                                this.msgSendMsg = "未知错误!";
                                return false;
                        }
                    } catch (Exception e) {
                        String info = "【群发信息失败】【解析json错误】" + e.getMessage()
                                + "\n\t【文本:】\n\t" + text;
                        System.err.println(info);
                        log.debug(info);
                        log.info(info);
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            String info = "【群发信息失败】" + e.getMessage();
            System.err.println(info);
            log.debug(info);
            log.info(info);
            return false;
        }
        return false;
    }

转载注明地址:http://www.chengxuyuans.com/code/android/64198.html