JAVA模拟微信消息发送请求

JavaWeb模拟微信(网页版)发送消息到好友。PS:不是公众号,是好友之间,或者发送到群。

1、发送文字消息到好友,或群。

1、登录微信网页版,使用firebug抓取发送文字的请求:https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg

2、分析参数:

这里写图片描述
{\”BaseRequest\”:{\”Uin\”:123456,\”Sid\”:\”123456\”,\”Skey\”:\”@crypt_c799f4fc_7f98126a5836d9ed6fdbff40cd491c5f\”,\”DeviceID\”:\”e155964836553096\”},\”Msg\”:{\”Type\”:1,\”Content\”:\”hello\”,\”FromUserName\”:\”@abe6eb6ac61b71451c703f7126b8e454\”,\”ToUserName\”:\”abe6eb6ac61b71451c703f7126b8e454\”,\”LocalID\”:\”“+currentTimeMillis+”\”,\”ClientMsgId\”:\”“+currentTimeMillis+”\”}}
uin:表示微信帐号id,sid:每次会话生成新的,sky:每次会话生成新的,FromUserName:消息发送人,ToUserName:消息接收人,DeviceID:可重复

示例代码:

package com.klay.wewhat;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;

/**
 * send text
 * @author klay
 *
 */
public class SendText {

    public static String requestUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg";
    public static String cookie  = "";

    public static void sendMsg(String requestUrl,String cookie){
        String url = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg";//请求链接
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        post.addHeader(new BasicHeader("cookie", cookie)); //其实cookie可以不设置,也能发送
        Long currentTimeMillis = System.currentTimeMillis();
        String requestParam = "{\"BaseRequest\":{\"Uin\":You id,\"Sid\":\"EwwSa9Jb1fSGjQW1\",\"Skey\":\"@crypt_c799f4fc_7f98126a5836d9ed6fdbff40cd491c5f\",\"DeviceID\":\"e155964836553096\"},\"Msg\":{\"Type\":1,\"Content\":\"hello.J\",\"FromUserName\":\"@123\",\"ToUserName\":\"123\",\"LocalID\":\""+currentTimeMillis+"\",\"ClientMsgId\":\""+currentTimeMillis+"\"}}";
        try {
            StringEntity s = new StringEntity(requestParam);
            post.setEntity(s);
            HttpResponse res = client.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                HttpEntity entity = res.getEntity();
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        sendMsg(requestUrl,cookie);
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值