JAVA模拟微信消息发送请求

来源:互联网 发布:原材料追溯软件 编辑:程序博客网 时间:2024/04/29 14:35

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 0