处理微信服务器发来的消息

package com.router.api.wechat.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.router.api.wechat.service.CoreService;
import com.router.api.wechat.util.Tool;
import com.router.service.AccessTokenService;

@WebServlet(name="Auth",urlPatterns="/Auth")
public class CoreController extends HttpServlet{
  @Autowired
  private AccessTokenService accessTokenService;
 /**
  * 本类 是为了验证token
  *
  */
 private static final long serialVersionUID = 4440739483644821986L;

 /**
  * 请求校验(确认请求来自微信服务器)
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // 微信加密签名
  String signature = request.getParameter("signature");
  // 时间戳
  String timestamp = request.getParameter("timestamp");
  // 随机数
  String nonce = request.getParameter("nonce");
  // 随机字符串
  String echostr = request.getParameter("echostr");

  PrintWriter out = response.getWriter();
  // 请求校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
  if (Tool.checkSignature(signature, timestamp, nonce)) {
   out.print(echostr);
  }
  out.close();
  out = null;
  System.out.println("123131d31a23d1a31sd32a1d3a1d31ad54a6d4");
 }

 /**
  * 处理微信服务器发来的消息
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 消息的接收、处理、响应
        // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
     request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        // 调用核心业务类接收消息、处理消息
        PrintWriter out = response.getWriter();
        String respXml = CoreService.processRequest(request);

//   设置了utf-8 不管用的时候 可以试试
        out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        out.println(respXml);
        out.close();
        out=null;
 
 }
}

你可能感兴趣的:(处理微信服务器发来的消息)