Micro-Credit Public Account development Tutorial (v) receiving and responding to various messages

Source: Internet
Author: User

In the previous article we have encapsulated messages and related operations in the micro-trust platform interface, which will mainly describe how to receive and respond to messages sent by a micro-trust server.

Clear where to receive messages

From the micro-credit public Platform Interface Message Guide, you can learn that when a user sends a message to a public account, the micro-mail server submits the message to the URL that we filled out in the interface configuration information by post. And we need to receive messages, process messages, and respond messages in the Dopost method of the request-handling class Coreservlet the URL points to.

Receive, process, respond to messages

Let's look at the complete code for the Coreservlet I've written:

Package org.liufeng.course.servlet;  
Import java.io.IOException;  
      
Import Java.io.PrintWriter;  
Import javax.servlet.ServletException;  
Import Javax.servlet.http.HttpServlet;  
Import Javax.servlet.http.HttpServletRequest;  
      
Import Javax.servlet.http.HttpServletResponse;  
Import Org.liufeng.course.service.CoreService;  
      
Import Org.liufeng.course.util.SignUtil; /** * Core Request Processing class * * @author Liufeng * @date 2013-05-18/public class Coreservlet extends HttpServlet {PR  
      
    Ivate static final Long serialversionuid = 4440739483644821986L; 

/** * Confirmation request from micro-trust server/public void doget (HttpServletRequest request, httpservletresponse response) throws  
        Servletexception, IOException {//micro-letter Encrypted signature String signature = request.getparameter ("signature");  
        Timestamp String timestamp = request.getparameter ("timestamp");  
      Random number String nonce = Request.getparameter ("nonce");  Random string echostr = Request.getparameter ("Echostr");  
        PrintWriter out = Response.getwriter (); Verify the request through the inspection signature, if the verification success is returned as the ECHOSTR, indicating the successful access, otherwise access failure if (signutil.checksignature (signature, timestamp, nonc  
        e)) {out.print (ECHOSTR);  
        } out.close ();  
    out = null; /** * Processing the message from the micro-trust server * * public void DoPost (HttpServletRequest request, HttpServletResponse re Sponse) throws Servletexception, IOException {//The request, the response encoding are set to UTF-8 (to prevent Chinese garbled) Request.setcharacterenc  
        Oding ("UTF-8");  
      
        Response.setcharacterencoding ("UTF-8");  
              
        Call the core business class to receive the message, process the message String Respmessage = coreservice.processrequest (request);  
        Response message PrintWriter out = Response.getwriter ();  
        Out.print (Respmessage);  
    Out.close (); }  
      
}

Code Description:

1 The 51st line of code: Micro-trust server Post message is used UTF-8 code, in the reception will also use the same code, otherwise Chinese will garbled;

2 line 52nd code: In response to the message (reply to the message to the user), also set the encoding mode to UTF-8, the principle of the same;

3 Line 54th code: Call the Coreservice class ProcessRequest method to receive, process the message, and get the processing result;

4 line 57th to 59th: Call Response.getwriter (). The Write () method returns the processing result of the message to the user

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.