您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關微信二次開發之文本消息請求與發送的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
做微信文本消息接口請求與發送,具體內容如下
需要導入庫:dom4j-1.6.1.jar,xstream-1.3.1.jar
第一步:新建包com.wtz.message.response,新建類BaseMessage.java
package com.wtz.message.response; /** * @author wangtianze QQ:864620012 * @date 2017年4月19日 下午3:12:40 * <p>version:1.0</p> * <p>description:基礎消息類</p> */ public class BaseMessage { //接收方 private String ToUserName; //發送方 private String FromUserName; //消息的創建時間 private long CreateTime; //消息類型 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; } }
第二步:找到包com.wtz.message.response,新建類TextMessage.java
package com.wtz.message.response; /** * @author wangtianze QQ:864620012 * @date 2017年4月19日 下午3:22:33 * <p>version:1.0</p> * <p>description:文本消息類</p> */ public class TextMessage extends BaseMessage{ //消息內容 private String Content; public String getContent() { return Content; } public void setContent(String content) { Content = content; } }
第三步:找到包com.wtz.util,新建類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 2017年4月19日 下午3:29:58 * <p>version:1.0</p> * <p>description:消息處理工具類</p> */ public class MessageUtil { //定義了消息類型(常量:文本類型) public static final String RESP_MESSAGE_TYPE_TEXT = "text"; //從流中解析出每個節點的內容 public static Map<String,String> parseXml(HttpServletRequest request) throws IOException{ Map<String,String> map = new HashMap<String,String>(); //從輸入流中獲取流對象 InputStream in = request.getInputStream(); //構建SAX閱讀器對象 SAXReader reader = new SAXReader(); try { //從流中獲得文檔對象 Document doc = reader.read(in); //獲得根節點 Element root = doc.getRootElement(); //獲取根節點下的所有子節點 List<Element> children = root.elements(); for(Element e:children){ //遍歷每一個節點,并按照節點名--節點值放入map中 map.put(e.getName(), e.getText()); System.out.println("用戶發送的消息XML解析為:" + e.getName() + e.getText()); } //關閉流 in.close(); in = null; } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } return map; } /** * 用于擴展節點數據按照<ToUserName><![CDATA[toUser]]></ToUserName>,中間加了CDATA段 */ 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("<![CDATA["); writer.write(text); writer.write("]]>"); }else{ writer.write(text); } } }; } }); /** * 將文本消息轉換成XML格式 */ public static String messageToXml(TextMessage textMessage){ xstream.alias("xml",textMessage.getClass()); String xml = xstream.toXML(textMessage); System.out.println("響應所轉換的XML:"+xml); return xml; } }
第四步:找到包com.wtz.service,新建類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 2017年4月19日 下午8:04:14 * <p>version:1.0</p> * <p>description:核心服務類</p> */ public class ProcessService { public static String dealRequest(HttpServletRequest request) throws IOException{ //響應的XML串 String respXml = ""; //要響應的文本內容 String respContent = "未知的消息類型"; Map<String,String> 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("用戶給公眾號發的消息為:" + Content); //構建一條文本消息 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 = "王天澤的公眾號收到了您的一條文本消息:" + Content + ",時間戳是:" + (new Date().getTime()); } textMessage.setContent(respContent); respXml = MessageUtil.messageToXml(textMessage); System.out.println("respXml:"+respXml); return respXml; } }
第五步:找到包com.wtz.service下的LoginServlet類,重寫doPost方法
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 2017年4月17日 下午8:11:32 * <p>version:1.0</p> * <p>description:微信請求驗證類</p> */ public class LoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("get請求。。。。。。"); //1.獲得微信簽名的加密字符串 String signature = request.getParameter("signature"); //2.獲得時間戳信息 String timestamp = request.getParameter("timestamp"); //3.獲得隨機數 String nonce = request.getParameter("nonce"); //4.獲得隨機字符串 String echostr = request.getParameter("echostr"); System.out.println("獲得微信簽名的加密字符串:"+signature); System.out.println("獲得時間戳信息:"+timestamp); System.out.println("獲得隨機數:"+nonce); System.out.println("獲得隨機字符串:"+echostr); PrintWriter out = response.getWriter(); //驗證請求確認成功原樣返回echostr參數內容,則接入生效,成為開發者成功,否則失敗 if(ValidationUtil.checkSignature(signature, timestamp, nonce)){ out.print(echostr); } out.close(); out = null; } /** * 接受微信服務器發過來的XML數據包(通過post請求發送過來的) */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //獲取微信加密的簽名字符串 String signature = request.getParameter("signature"); //獲取時間戳 String timestamp = request.getParameter("timestamp"); //獲取隨機數 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; } }
感謝各位的閱讀!關于“微信二次開發之文本消息請求與發送的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。