微信企业号开启接口

1        企业号开启回调模式(参考资料http://www.youkuaiyun.com/article/2014-10-09/2822004-weixin)



其中URL包含已经备案好的域名和项目CoreServlet的请求路径。

Token:保持和代码中的token一致即可。

EncodingAESKey…:微信随机产生(要和代码中保持一致)



Java代码这样写(还有很多辅助的工具类可以去百度)

publicclass CoreServlet extends HttpServlet{

         /**

          *

          */

         private static final longserialVersionUID = 4440739483644821986L;

        

         @Override

         protected void doGet(HttpServletRequestrequest, HttpServletResponse response) throws ServletException, IOException {

                   // 微信加密签名 

        String msg_signature =request.getParameter("msg_signature"); 

        // 时间戳 

        String timestamp =request.getParameter("timestamp"); 

        // 随机数 

        String nonce =request.getParameter("nonce"); 

        // 随机字符串 

        String echostr =request.getParameter("echostr"); 

        // 打印请求地址

        System.out.println("request="+ request.getRequestURL()); 

        // 流

        PrintWriter out =response.getWriter(); 

        // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败 

        String result=null; 

        try

        { 

                 WXBizMsgCryptwxcpt = newWXBizMsgCrypt(ParamesAPI.token,ParamesAPI.encodingAESKey,ParamesAPI.corpId); 

                 //验证URL函数

                 result= wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr);

                 System.out.println("verifyurlechostr: " + result);

                 //验证URL成功,将sEchoStr返回

                 //HttpUtils.SetResponse();

        }

        catch (AesException e) { 

            e.printStackTrace(); 

        } 

        if (result == null) { 

                 //result为空,赋予token

                 result= ParamesAPI.token;

        }

      

       

        // 拼接请求参数

        String str = msg_signature+""+timestamp+" "+nonce+" "+echostr;

        // 打印参数+地址+result

        System.out.println("Exception:"+result+""+ request.getRequestURL()+" "+"FourParames:"+str);

        out.print(result);

        out.close(); 

        out = null; 

         }

         @Override

         protected voiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {

//               将请求、响应的编码均设置为UTF-8(防止中文乱码)

                   request.setCharacterEncoding("UTF-8");

                   response.setCharacterEncoding("UTF-8");

 

                   // 微信加密签名

                   String msg_signature =request.getParameter("msg_signature");

                   // 时间戳

                   String timestamp =request.getParameter("timestamp");

                   // 随机数

                   String nonce =request.getParameter("nonce");

                  

                   //从请求中读取整个post数据

                   InputStream inputStream =request.getInputStream();

                   //commons.io.jar 方法

                   String Post =IOUtils.toString(inputStream, "UTF-8");

                   // Post打印结果

                   System.out.println(Post);

                  

                   String Msg = "";

                   WXBizMsgCrypt wxcpt = null;

                   try {

                            wxcpt = newWXBizMsgCrypt(ParamesAPI.token,ParamesAPI.encodingAESKey,ParamesAPI.corpId);

                            //解密消息

                            Msg =wxcpt.DecryptMsg(msg_signature, timestamp, nonce, Post);

                   } catch (AesException e) {

                            e.printStackTrace();

                   }

                   // Msg打印结果

                   System.out.println("Msg打印结果:" + Msg);

                  

                   // 调用核心业务类接收消息、处理消息

                   String respMessage =CoreService.processRequest(Msg);

                  

                   // respMessage打印结果

                   System.out.println("respMessage打印结果:" + respMessage);

                   String encryptMsg ="";

                   try {

                            //加密回复消息

                            encryptMsg =wxcpt.EncryptMsg(respMessage, timestamp, nonce);

                   } catch (AesException e) {

                            e.printStackTrace();

                   }

                  

                   // 响应消息

                   PrintWriter out =response.getWriter();

                   out.print(encryptMsg);

                   out.close();

         }

以上是一个原始的servlet方法,在微信回调模式URL中拼写 “域名+项目名+servlet”即可,这里要说明微信小助手只是我这里演示使用,实际开发情况并不符合要求!!!
本人也是第一在没有别人帮助的情况下,完成微信一系列的开发(当然百度看过很多文章),因能力有限文章写得不够完整请见谅!有问题提出来尽量帮助各位
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值