zoukankan      html  css  js  c++  java
  • SSH框架构建微信公众帐号服务器小技巧

    SSH框架构建微信公众帐号服务器小技巧

      熟悉struts2和servlet的同学应该清楚,struts2的方法多样性弥补了servlet单一的doGet 和doPost方法。如果自己的公众账号服务器是用servlet跟微信服务器交互,是用doGet方法完成与微信服务器的校验,用doPost方法接收用户发来的信息,经由微信服务器传递的HTTP POST请求。

      

    例一:servlet中与微信服务器的校验和用户发送信息的处理

      java代码:

     1 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     2      // 微信加密签名
     3       String signature = request.getParameter("signature");
     4  
     5      // 时间戳
     6      String timestamp = request.getParameter("timestamp");
     7  
     8      // 随机数
     9      String nonce = request.getParameter("nonce");
    10  
    11      // 随机字符串
    12      String echostr = request.getParameter("echostr");
    13  
    14      PrintWriter out = response.getWriter();
    15  
    16      // 请求校验,若校验成功则原样返回echostr,表示接入成功,否则接入        失败
    17      if (SignUtil.checkSignature(signature, timestamp, nonce)) {
    18      
    19          out.print(echostr);
    20     
    21       }
    22 
    23      out.close();
    24      out = null;
    25  }
    26  
    27  
    28  
    29  
    30  /**
    31  *doPost处理用户所发送到服务器的信息
    32  */
    33  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    34          // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
    35          request.setCharacterEncoding("UTF-8");
    36          response.setCharacterEncoding("UTF-8");
    37  
    38          // 接收参数微信加密签名、 时间戳、随机数
    39          String signature = request.getParameter("signature");
    40          String timestamp = request.getParameter("timestamp");
    41          String nonce = request.getParameter("nonce");
    42          PrintWriter out = response.getWriter();
    43  
    44          // 请求校验
    45         if (SignUtil.checkSignature(signature, timestamp, nonce)) {
    46 
    47              // 调用核心服务类接收处理请求
    48              String respXml = CoreService.processRequest(request);
    49              out.print(respXml);
    50      }
    51  
    52          out.close();
    53          out = null;
    54  }    

    但是如果是struts2交互问题就出来,因为你只能用 url:http://xxx.xxxx.com/xxx.do  和 token:xxx  注册微信公众平台

    这样注册问题就出来了,你要同时响应get跟post请求。所以你需要对servlet的doGet和doPost方法进行整合,整合到action中的一个方法里。

      

     这样就有了如下代码:

     列2 :struts2中处理微信服务器校验 和用户发来消息处理

     java中struts2 action中代码:

     

     1 /**
     2   *action中的处理方法
     3   */
     4    public String execute() throws Exception {
     5   
     6         // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
     7         HttpServletRequest request =             ServletActionContext.getRequest();
     8        HttpServletResponse response = ServletActionContext.getResponse();
     9        request.setCharacterEncoding("UTF-8");
    10        response.setCharacterEncoding("UTF-8");
    11 
    12        // 接收参数微信加密签名、 时间戳、随机数
    13        String signature = request.getParameter("signature");
    14        String timestamp = request.getParameter("timestamp");
    15        String nonce = request.getParameter("nonce");
    16  
    17        // 随机字符串
    18        String echostr = request.getParameter("echostr");
    19        // System.out.println(signature+"...............................");
    20        PrintWriter out = response.getWriter();
    21  
    22        // 请求校验
    23        if (SignUtil.checkSignature(signature, timestamp, nonce)) {
    24          String method = ServletActionContext.getRequest().getMethod();
    25 
    26          if (method.equals("POST")) {
    27  
    28              // 调用核心服务类接收处理请求
    29              String respXml = CoreService.processRequest(request);
    30              out.print(respXml);
    31  
    32          } else {
    33  
    34              out.print(echostr);
    35  
    36          }
    37  
    38        }
    39  
    40        out.close();
    41        out = null;
    42        return null;
    43      }

    上述代码红色部分为核心核心部分,应该很好理解的;

    关于上述两个类中的 SignUtil 工具类 没有提供代码,本帖只做如何与微信服务器交互的处理,也方便日后自己可以看看。

  • 相关阅读:
    JS数组及操作方法,堆和栈的概念
    JS变量作用域,浏览器预解析
    JS函数的基本概念
    JS循环语句
    JS程序三大结构及语法语句
    src与href
    JS概念及基本语法
    图片整合技术
    hdu6395 Sequence(分段矩阵快速幂)
    hdu6396 Swordsman(贪心)
  • 原文地址:https://www.cnblogs.com/kzhan/p/4155514.html
Copyright © 2011-2022 走看看