Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Wechat developer handle messages from Wechat clients?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the development of Wechat how to deal with messages sent by Wechat client, the article is very detailed, has a certain reference value, interested friends must read it!

When we enable Wechat developer mode, we configure a URL address. When we submit to enable Wechat developer mode, Tencent's Wechat server will send a get request to the URL address with some parameters for verification. When it comes to get requests, we must talk about post requests, follow the messages and events triggered by Wechat fans on our official account. Tencent's Wechat server will send an post request to this URL address, and the content of the request is a string in the form of xml documents.

Therefore, the processing method of the get request of the URL address is specially used to open the Wechat developer mode, while the post request is used to handle messages sent to us by Wechat fans or triggered events, so the starting point of our later Wechat development work is the post handling method of the URL address.

Let's deal with the simplest example: a fan sends any text message to us, and we reply to him with a message: "Hello, + his Wechat openId"

Paste the code directly below:

Processing servlet for URL:

Public class CoreServlet extends HttpServlet {private static final long serialVersionUID = 4440739483644821986L; / * request verification (confirm that the request comes from Wechat server) * / public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / the encrypted signature sent by Wechat server String signature = request.getParameter ("signature") / / timestamp String timestamp = request.getParameter ("timestamp"); / / random number String nonce = request.getParameter ("nonce"); / / random string String echostr = request.getParameter ("echostr"); PrintWriter out = response.getWriter () / / request verification. If the verification is successful, echostr will be returned as is, which means the connection is successful. Otherwise, the connection failed if (SignUtil.checkSignature (signature, timestamp, nonce)) {out.print (echostr);} out.close (); out = null } / * request verification and processing * / public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / set the encoding of request and response to UTF-8 (prevent Chinese garbled) request.setCharacterEncoding ("UTF-8"); response.setCharacterEncoding ("UTF-8") / / receive parameters Wechat encryption signature, timestamp, random number String signature = request.getParameter ("signature"); String timestamp = request.getParameter ("timestamp"); String nonce = request.getParameter ("nonce"); PrintWriter out = response.getWriter () / / request if (SignUtil.checkSignature (signature, timestamp, nonce)) {Message msgObj = XMLUtil.getMessageObject (request) / / read the message (xml string) sent by Wechat client And convert it to the message object if (msgObj! = null) {String xml = "" + "" + / / recipient account (received OpenID) " "+ / / developer Wechat number" 12345678 "+" Out.write (xml); / / reply to the message (xml string) from Wechat client out.close (); return;}} out.write ("") Out.close ();}}

Xml string processing tool class to implement the conversion of xml messages to message objects:

Public class XMLUtil {/ * read the message content sent by the user to the official account from request * @ param request * @ return the message content sent by the user to the official account * @ throws IOException * / public static String readRequestContent (HttpServletRequest request) throws IOException {/ / read the returned message from the input stream Allow InputStream inputStream = request.getInputStream () InputStreamReader inputStreamReader = new InputStreamReader (inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader (inputStreamReader); String str = null; StringBuilder buffer = new StringBuilder (); while ((str = bufferedReader.readLine ())! = null) {buffer.append (str) } / / release resources bufferedReader.close (); inputStreamReader.close (); inputStream.close (); return buffer.toString () } / * convert the contents of the xml document into map * @ param xmlDoc * @ return map * / public static Map xmlToMap (String xmlDoc) {/ / create a new string StringReader read = new StringReader (xmlDoc) / / create a new input source SAX parser will use the InputSource object to determine how to read the XML input InputSource source = new InputSource (read); / / create a new SAXBuilder SAXBuilder sb = new SAXBuilder (); Map xmlMap = new HashMap (); try {Document doc = sb.build (source) / / construct a Document Element root = doc.getRootElement () through the input source; / / take the root element List cNodes = root.getChildren (); / / get the collection of all the child elements of the root element (the child node of the root element, excluding the grandchild node) Element et = null; for (int iNode

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report