官方指南
https://mp.weixin.qq.com/wiki/1/5dc395cdeb98e9d23a8541cf0bab38ad.html
下载示例代码
https://mp.weixin.qq.com/wiki/static/assets/a5a22f38cb60228cb32ab61d9e4c414b.zip
需要里面的文件
开始之前是建立在服务器接入成功的情况下
微信会把公众号上的信息用post方式提交到设置的url
所以我在servlet里doPost处理请求
doPost方法
protectedvoid doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8");//设置编码 try { //签名串 String msgSignature = request.getParameter("msg_signature"); // 时间戳 String timestamp = request.getParameter("timestamp"); // 随机数 String nonce = request.getParameter("nonce");
//接收密文并解密 WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId); String msgData = getMsgData(request); //流数据转成string String resultXml = pc.decryptMsg(msgSignature, timestamp, nonce, msgData); //解密 //dom4j将xml数据转成map SAXReader saxreader = new SAXReader(); Document doc = saxreader.read(new ByteArrayInputStream(resultXml.getBytes("UTF-8"))); Map<String, String> map = new HashMap<String, String>(); |