In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The purpose of this article is to share with you the content of the sample analysis of the text message request and transmission of the secondary development of Wechat. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Request and send Wechat text message API, the specific contents are as follows
Need to import library: dom4j-1.6.1.jar,xstream-1.3.1.jar
Step 1: create a new package com.wtz.message.response and a new class BaseMessage.java
Package com.wtz.message.response;/** * @ author wangtianze QQ:864620012 * @ date April 19, 2017 3:12:40 *
Version:1.0
*
Description: basic message class
* / public class BaseMessage {/ / receiver private String ToUserName; / / creation time of the sender private String FromUserName; / / message type private String MsgType; public String getToUserName () {return ToUserName;} public void setToUserName (String toUserName) {ToUserName = toUserName;} public String getFromUserName () {return FromUserName;} public void setFromUserName (String fromUserName) {FromUserName = fromUserName;} public long getCreateTime () {return CreateTime;} public void setCreateTime (long createTime) {CreateTime = createTime } public String getMsgType () {return MsgType;} public void setMsgType (String msgType) {MsgType = msgType;}}
Step 2: find the package com.wtz.message.response and create a new class TextMessage.java
Package com.wtz.message.response;/** * @ author wangtianze QQ:864620012 * @ date April 19, 2017 3:22:33 *
Version:1.0
*
Description: text message class
* / public class TextMessage extends BaseMessage {/ / message content private String Content; public String getContent () {return Content;} public void setContent (String content) {Content = content;}}
Step 3: find the package com.wtz.util and create a new class MessageUtil.java
Package com.wtz.util;import java.io.IOException;import java.io.InputStream;import java.io.Writer;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.core.util.QuickWriter;import com.thoughtworks.xstream.io.HierarchicalStreamWriter Import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;import com.thoughtworks.xstream.io.xml.XppDriver;import com.wtz.message.response.TextMessage;/** * @ author wangtianze QQ:864620012 * @ date April 19, 2017 3:29:58 *
Version:1.0
*
Description: message processing tool class
* / public class MessageUtil {/ / defines the message type (constant: text type) public static final String RESP_MESSAGE_TYPE_TEXT = "text"; / / parses the content of each node public static Map parseXml (HttpServletRequest request) throws IOException {Map map = new HashMap () from the stream; / / gets the stream object InputStream in = request.getInputStream () from the input stream; / / builds the SAX reader object SAXReader reader = new SAXReader () Try {/ / get the document object Document doc = reader.read (in) from the stream; / / get the root node Element root = doc.getRootElement (); / / get all the child nodes under the root node List children = root.elements (); for (Element e:children) {/ / traverse each node and put map.put (e.getName (), e.getText ()) in the map according to the node name-node value System.out.println ("the message sent by the user XML parses to:" + e.getName () + e.getText ());} / / closes the stream in.close (); in = null;} catch (DocumentException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} return map } / * is used to expand node data in accordance with the CDATA segment * / private static XStream xstream = new XStream (new XppDriver () {public HierarchicalStreamWriter createWriter (Writer out) {return new PrettyPrintWriter (out) {boolean cdata = true; public void startNode (String name,Class clazz) {super.startNode (name,clazz);} protected void writeText (QuickWriter writer,String text) {if (cdata) {writer.write (")) } else {writer.write (text);};}}); / * convert text messages to XML format * / public static String messageToXml (TextMessage textMessage) {xstream.alias ("xml", textMessage.getClass ()); String xml = xstream.toXML (textMessage); System.out.println ("respond to converted XML:" + xml); return xml;}
Step 4: find the package com.wtz.service and create a new class ProcessService.java
Package com.wtz.util;import java.io.IOException;import java.util.Date;import java.util.Map;import javax.servlet.http.HttpServletRequest;import com.wtz.message.response.TextMessage;/** * @ author wangtianze QQ:864620012 * @ date April 19, 2017 8:04:14 *
Version:1.0
*
Description: core service class
* / public class ProcessService {public static String dealRequest (HttpServletRequest request) throws IOException {/ / response XML string String respXml = ""; / / text content to respond String respContent = "unknown message type"; Map requestMap = MessageUtil.parseXml (request); String fromUserName = requestMap.get ("FromUserName"); String toUserName = requestMap.get ("ToUserName"); String MsgType = requestMap.get ("MsgType"); String Content = requestMap.get ("Content") System.out.println ("the message sent by the user to the official account is:" + Content); / / build a text message TextMessage textMessage = new TextMessage (); textMessage.setToUserName (fromUserName); textMessage.setFromUserName (toUserName); textMessage.setCreateTime (new Date (). GetTime ()); textMessage.setMsgType (MessageUtil.RESP_MESSAGE_TYPE_TEXT) If (MsgType.equals (MessageUtil.RESP_MESSAGE_TYPE_TEXT)) {respContent = "Wang Tianze's official account received a text message from you:" + Content + "with a timestamp of" + (new Date (). GetTime ());} textMessage.setContent (respContent); respXml = MessageUtil.messageToXml (textMessage); System.out.println ("respXml:" + respXml); return respXml;}}
Step 5: find the LoginServlet class under the package com.wtz.service and override the doPost method
Package com.wtz.service;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 com.wtz.util.MessageUtil;import com.wtz.util.ProcessService;import com.wtz.util.ValidationUtil;/** * @ author wangtianze QQ:864620012 * @ date 17 April 2017 8:11:32 *
Version:1.0
*
Description: Wechat request verification class
* / public class LoginServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println ("get request.") / / 1. Obtain the encrypted string String signature = request.getParameter ("signature") of Wechat signature; / / 2. Get the timestamp information String timestamp = request.getParameter ("timestamp"); / / 3. Get the random number String nonce = request.getParameter ("nonce"); / / 4. Get the random string String echostr = request.getParameter ("echostr"); System.out.println ("get the encrypted string for Wechat signature:" + signature); System.out.println ("get timestamp information:" + timestamp); System.out.println ("get random number:" + nonce); System.out.println ("get random string:" + echostr); PrintWriter out = response.getWriter () / / if the verification request is confirmed to be successful, the echostr parameter content is returned as is, then the connection takes effect and the developer succeeds, otherwise if (ValidationUtil.checkSignature (signature, timestamp, nonce)) {out.print (echostr);} out.close (); out = null } / * accept XML packets sent by Wechat server (sent via post request) * / @ Override protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding ("utf-8"); response.setCharacterEncoding ("utf-8"); / / get Wechat encrypted signature string String signature = request.getParameter ("signature") / / get the timestamp String timestamp = request.getParameter ("timestamp"); / / get the random number String nonce = request.getParameter ("nonce"); PrintWriter out = response.getWriter (); if (ValidationUtil.checkSignature (signature,timestamp,nonce)) {String respXml = ""; try {respXml = ProcessService.dealRequest (request);} catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace ();} out.print (respXml) } out.close (); out = null;}} Thank you for reading! This is the end of the article on "sample analysis of text message request and sending in the secondary development of Wechat". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it out for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.