JAVA模拟微信消息发送请求

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kalyzhang1/article/details/48999137

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);
    }

}
展开阅读全文

没有更多推荐了,返回首页