监听微信公众号消息,监听微信公众号推送

发布于:2022-11-06 ⋅ 阅读:(771) ⋅ 点赞:(0)

监听微信公众号消息

效果图如下:
在这里插入图片描述

在这里插入图片描述

环境要求

需要windows服务器,登陆上微信。获取到的消息是xml格式,需要解析内容获取自己想要的公众号推送的消息。

其他微信接口说明

从官方文档可以看出,他做到了简简单单,但没有做到明明白白。

那么接下来我首先说下文中的url,很多人都不知道这个url,其实他就是

当这些正准备好之后,直接上代码

//微信推送事件 url
@RequestMapping(value = "/wx", method = RequestMethod.GET)
@ResponseBody
public void get(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    boolean isGet = request.getMethod().toLowerCase().equals("get");
    if (isGet) {
        // 微信加密签名
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");
        // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
        if (signature != null && CheckoutUtil.checkSignature(signature, timestamp, nonce)) {
            try {
                boolean flag = CheckoutUtil.checkSignature(signature, timestamp, nonce);
                System.out.println(flag);
                PrintWriter print = response.getWriter();
                print.write(echostr);
                System.out.println(echostr);
                print.flush();
                print.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

这个方法是你配置Url的时候他会请求这个地址,验证地址的准确性。


网站公告

今日签到

点亮在社区的每一天
去签到