微信公眾平台開發教程Java版(三) 消息接收和發送


https://www.iteye.com/blog/tuposky-2017429

前面兩章已經介紹了如何接入微信公眾平台,這一章說說消息的接收和發送

可以先了解公眾平台的消息api接口(接收消息,發送消息)

http://mp.weixin.qq.com/wiki/index.php



 

接收消息

當普通微信用戶向公眾賬號發消息時,微信服務器將POST消息的XML數據包到開發者填寫的URL上。

 

 http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E6%99%AE%E9%80%9A%E6%B6%88%E6%81%AF

 

接收的消息類型有6種,分別為:

可以根據官方的api提供的字段建立對應的實體類

如:文本消息

 

有很多屬性是所有消息類型都需要的,可以把這些信息提取出來建立一個基類

 

Java代碼   收藏代碼
  1. package com.ifp.weixin.entity.Message.req;  
  2.   
  3. /** 
  4.  * 消息基類(用戶 -> 公眾帳號) 
  5.  *  
  6.  */  
  7. public class BaseMessage {  
  8.     /** 
  9.      * 開發者微信號 
  10.      */  
  11.     private String ToUserName;  
  12.     /** 
  13.      * 發送方帳號(一個OpenID) 
  14.      */  
  15.     private String FromUserName;  
  16.     /** 
  17.      * 消息創建時間 (整型) 
  18.      */  
  19.     private long CreateTime;  
  20.   
  21.     /** 
  22.      * 消息類型 text、image、location、link 
  23.      */  
  24.     private String MsgType;  
  25.   
  26.     /** 
  27.      * 消息id,64位整型 
  28.      */  
  29.     private long MsgId;  
  30.   
  31.     public String getToUserName() {  
  32.         return ToUserName;  
  33.     }  
  34.   
  35.     public void setToUserName(String toUserName) {  
  36.         ToUserName = toUserName;  
  37.     }  
  38.   
  39.     public String getFromUserName() {  
  40.         return FromUserName;  
  41.     }  
  42.   
  43.     public void setFromUserName(String fromUserName) {  
  44.         FromUserName = fromUserName;  
  45.     }  
  46.   
  47.     public long getCreateTime() {  
  48.         return CreateTime;  
  49.     }  
  50.   
  51.     public void setCreateTime(long createTime) {  
  52.         CreateTime = createTime;  
  53.     }  
  54.   
  55.     public String getMsgType() {  
  56.         return MsgType;  
  57.     }  
  58.   
  59.     public void setMsgType(String msgType) {  
  60.         MsgType = msgType;  
  61.     }  
  62.   
  63.     public long getMsgId() {  
  64.         return MsgId;  
  65.     }  
  66.   
  67.     public void setMsgId(long msgId) {  
  68.         MsgId = msgId;  
  69.     }  
  70.   
  71. }  

 接收的文本消息

 

Java代碼   收藏代碼
  1. package com.ifp.weixin.entity.Message.req;  
  2.   
  3. /** 
  4.  * 文本消息 
  5.  */  
  6. public class TextMessage extends BaseMessage {  
  7.     /** 
  8.      * 回復的消息內容 
  9.      */  
  10.     private String Content;  
  11.   
  12.     public String getContent() {  
  13.         return Content;  
  14.     }  
  15.   
  16.     public void setContent(String content) {  
  17.         Content = content;  
  18.     }  
  19. }  

 接收的圖片消息

Java代碼   收藏代碼
  1. package com.ifp.weixin.entity.Message.req;  
  2.   
  3. public class ImageMessage extends BaseMessage{  
  4.   
  5.     private String picUrl;  
  6.   
  7.     public String getPicUrl() {  
  8.         return picUrl;  
  9.     }  
  10.   
  11.     public void setPicUrl(String picUrl) {  
  12.         this.picUrl = picUrl;  
  13.     }  
  14.       
  15. }  

 

 

接收的鏈接消息

Java代碼   收藏代碼
  1. package com.ifp.weixin.entity.Message.req;  
  2.   
  3.   
  4. public class LinkMessage extends BaseMessage {  
  5.     /** 
  6.      * 消息標題 
  7.      */  
  8.     private String Title;  
  9.     /** 
  10.      * 消息描述 
  11.      */  
  12.     private String Description;  
  13.     /** 
  14.      * 消息鏈接 
  15.      */  
  16.     private String Url;  
  17.   
  18.     public String getTitle() {  
  19.         return Title;  
  20.     }  
  21.   
  22.     public void setTitle(String title) {  
  23.         Title = title;  
  24.     }  
  25.   
  26.     public String getDescription() {  
  27.         return Description;  
  28.     }  
  29.   
  30.     public void setDescription(String description) {  
  31.         Description = description;  
  32.     }  
  33.   
  34.     public String getUrl() {  
  35.         return Url;  
  36.     }  
  37.   
  38.     public void setUrl(String url) {  
  39.         Url = url;  
  40.     }  
  41.   
  42. }  

 

 接收的語音消息

 

Java代碼   收藏代碼
  1. package com.ifp.weixin.entity.Message.req;  
  2.   
  3. /** 
  4.  * 語音消息 
  5.  *  
  6.  * @author Caspar 
  7.  *  
  8.  */  
  9. public class VoiceMessage extends BaseMessage {  
  10.     /** 
  11.      * 媒體ID 
  12.      */  
  13.     private String MediaId;  
  14.     /** 
  15.      * 語音格式 
  16.      */  
  17.     private String Format;  
  18.   
  19.     public String getMediaId() {  
  20.         return MediaId;  
  21.     }  
  22.   
  23.     public void setMediaId(String mediaId) {  
  24.         MediaId = mediaId;  
  25.     }  
  26.   
  27.     public String getFormat() {  
  28.         return Format;  
  29.     }  
  30.   
  31.     public void setFormat(String format) {  
  32.         Format = format;  
  33.     }  
  34.   
  35. }  

 接收的地理位置消息

 

Java代碼   收藏代碼
  1. package com.ifp.weixin.entity.Message.req;  
  2.   
  3.   
  4. /** 
  5.  * 位置消息 
  6.  *  
  7.  * @author caspar 
  8.  *  
  9.  */  
  10. public class LocationMessage extends BaseMessage {  
  11.     /** 
  12.      * 地理位置維度 
  13.      */  
  14.     private String Location_X;  
  15.     /** 
  16.      * 地理位置經度 
  17.      */  
  18.     private String Location_Y;  
  19.   
  20.     /** 
  21.      * 地圖縮放大小 
  22.      */  
  23.     private String Scale;  
  24.   
  25.     /** 
  26.      * 地理位置信息 
  27.      */  
  28.     private String Label;  
  29.   
  30.     public String getLocation_X() {  
  31.         return Location_X;  
  32.     }  
  33.   
  34.     public void setLocation_X(String location_X) {  
  35.         Location_X = location_X;  
  36.     }  
  37.   
  38.     public String getLocation_Y() {  
  39.         return Location_Y;  
  40.     }  
  41.   
  42.     public void setLocation_Y(String location_Y) {  
  43.         Location_Y = location_Y;  
  44.     }  
  45.   
  46.     public String getScale() {  
  47.         return Scale;  
  48.     }  
  49.   
  50.     public void setScale(String scale) {  
  51.         Scale = scale;  
  52.     }  
  53.   
  54.     public String getLabel() {  
  55.         return Label;  
  56.     }  
  57.   
  58.     public void setLabel(String label) {  
  59.         Label = label;  
  60.     }  
  61.   
  62. }  

 

 

發送被動響應消息

    對於每一個POST請求,開發者在響應包(Get)中返回特定XML結構,對該消息進行響應(現支持回復文本、圖片、圖文、語音、視頻、音樂)。請注意,回復圖片等多媒體消息時需要預先上傳多媒體文件到微信服務器,只支持認證服務號。

 

    同樣,建立響應消息的對應實體類

    也把公共的屬性提取出來,封裝成基類

 

     響應消息的基類

Java代碼   收藏代碼
  1. package com.ifp.weixin.entity.Message.resp;  
  2.   
  3. /** 
  4.  * 消息基類(公眾帳號 -> 用戶) 
  5.  */  
  6. public class BaseMessage {  
  7.       
  8.     /** 
  9.      * 接收方帳號(收到的OpenID) 
  10.      */  
  11.     private String ToUserName;  
  12.     /** 
  13.      * 開發者微信號 
  14.      */  
  15.     private String FromUserName;  
  16.     /** 
  17.      * 消息創建時間 (整型) 
  18.      */  
  19.     private long CreateTime;  
  20.       
  21.     /** 
  22.      * 消息類型 
  23.      */  
  24.     private String MsgType;  
  25.       
  26.     /** 
  27.      * 位0x0001被標志時,星標剛收到的消息 
  28.      */  
  29.     private int FuncFlag;  
  30.   
  31.     public String getToUserName() {  
  32.         return ToUserName;  
  33.     }  
  34.   
  35.     public void setToUserName(String toUserName) {  
  36.         ToUserName = toUserName;  
  37.     }  
  38.   
  39.     public String getFromUserName() {  
  40.         return FromUserName;  
  41.     }  
  42.   
  43.     public void setFromUserName(String fromUserName) {  
  44.         FromUserName = fromUserName;  
  45.     }  
  46.   
  47.     public long getCreateTime() {  
  48.         return CreateTime;  
  49.     }  
  50.   
  51.     public void setCreateTime(long createTime) {  
  52.         CreateTime = createTime;  
  53.     }  
  54.   
  55.     public String getMsgType() {  
  56.         return MsgType;  
  57.     }  
  58.   
  59.     public void setMsgType(String msgType) {  
  60.         MsgType = msgType;  
  61.     }  
  62.   
  63.     public int getFuncFlag() {  
  64.         return FuncFlag;  
  65.     }  
  66.   
  67.     public void setFuncFlag(int funcFlag) {  
  68.         FuncFlag = funcFlag;  
  69.     }  
  70. }  

 

 

    響應文本消息

   

Java代碼   收藏代碼
  1. package com.ifp.weixin.entity.Message.resp;  
  2.   
  3.   
  4. /** 
  5.  * 文本消息 
  6.  */  
  7. public class TextMessage extends BaseMessage {  
  8.     /** 
  9.      * 回復的消息內容 
  10.      */  
  11.     private String Content;  
  12.   
  13.     public String getContent() {  
  14.         return Content;  
  15.     }  
  16.   
  17.     public void setContent(String content) {  
  18.         Content = content;  
  19.     }  
  20. }  

 

 

響應圖文消息

   

Java代碼   收藏代碼
  1. package com.ifp.weixin.entity.Message.resp;  
  2.   
  3. import java.util.List;  
  4.   
  5. /** 
  6.  * 多圖文消息, 
  7.  * 單圖文的時候 Articles 只放一個就行了 
  8.  * @author Caspar.chen 
  9.  */  
  10. public class NewsMessage extends BaseMessage {  
  11.     /** 
  12.      * 圖文消息個數,限制為10條以內 
  13.      */  
  14.     private int ArticleCount;  
  15.     /** 
  16.      * 多條圖文消息信息,默認第一個item為大圖 
  17.      */  
  18.     private List<Article> Articles;  
  19.   
  20.     public int getArticleCount() {  
  21.         return ArticleCount;  
  22.     }  
  23.   
  24.     public void setArticleCount(int articleCount) {  
  25.         ArticleCount = articleCount;  
  26.     }  
  27.   
  28.     public List<Article> getArticles() {  
  29.         return Articles;  
  30.     }  
  31.   
  32.     public void setArticles(List<Article> articles) {  
  33.         Articles = articles;  
  34.     }  
  35. }  

 圖文消息的定義

 

 

Java代碼   收藏代碼
  1. package com.ifp.weixin.entity.Message.resp;  
  2.   
  3. /** 
  4.  * 圖文消息 
  5.  *  
  6.  */  
  7. public class Article {  
  8.     /** 
  9.      * 圖文消息名稱 
  10.      */  
  11.     private String Title;  
  12.   
  13.     /** 
  14.      * 圖文消息描述 
  15.      */  
  16.     private String Description;  
  17.   
  18.     /** 
  19.      * 圖片鏈接,支持JPG、PNG格式,<br> 
  20.      * 較好的效果為大圖640*320,小圖80*80 
  21.      */  
  22.     private String PicUrl;  
  23.   
  24.     /** 
  25.      * 點擊圖文消息跳轉鏈接 
  26.      */  
  27.     private String Url;  
  28.   
  29.     public String getTitle() {  
  30.         return Title;  
  31.     }  
  32.   
  33.     public void setTitle(String title) {  
  34.         Title = title;  
  35.     }  
  36.   
  37.     public String getDescription() {  
  38.         return null == Description ? "" : Description;  
  39.     }  
  40.   
  41.     public void setDescription(String description) {  
  42.         Description = description;  
  43.     }  
  44.   
  45.     public String getPicUrl() {  
  46.         return null == PicUrl ? "" : PicUrl;  
  47.     }  
  48.   
  49.     public void setPicUrl(String picUrl) {  
  50.         PicUrl = picUrl;  
  51.     }  
  52.   
  53.     public String getUrl() {  
  54.         return null == Url ? "" : Url;  
  55.     }  
  56.   
  57.     public void setUrl(String url) {  
  58.         Url = url;  
  59.     }  
  60.   
  61. }  

 

 

響應音樂消息

 

Java代碼   收藏代碼
  1. package com.ifp.weixin.entity.Message.resp;  
  2.   
  3.   
  4.   
  5. /** 
  6.  * 音樂消息 
  7.  */  
  8. public class MusicMessage extends BaseMessage {  
  9.     /** 
  10.      * 音樂 
  11.      */  
  12.     private Music Music;  
  13.   
  14.     public Music getMusic() {  
  15.         return Music;  
  16.     }  
  17.   
  18.     public void setMusic(Music music) {  
  19.         Music = music;  
  20.     }  
  21. }  

 

 

音樂消息的定義

Java代碼   收藏代碼
  1. package com.ifp.weixin.entity.Message.resp;  
  2.   
  3. /** 
  4.  * 音樂消息 
  5.  */  
  6. public class Music {  
  7.     /** 
  8.      * 音樂名稱 
  9.      */  
  10.     private String Title;  
  11.       
  12.     /** 
  13.      * 音樂描述 
  14.      */  
  15.     private String Description;  
  16.       
  17.     /** 
  18.      * 音樂鏈接 
  19.      */  
  20.     private String MusicUrl;  
  21.       
  22.     /** 
  23.      * 高質量音樂鏈接,WIFI環境優先使用該鏈接播放音樂 
  24.      */  
  25.     private String HQMusicUrl;  
  26.   
  27.     public String getTitle() {  
  28.         return Title;  
  29.     }  
  30.   
  31.     public void setTitle(String title) {  
  32.         Title = title;  
  33.     }  
  34.   
  35.     public String getDescription() {  
  36.         return Description;  
  37.     }  
  38.   
  39.     public void setDescription(String description) {  
  40.         Description = description;  
  41.     }  
  42.   
  43.     public String getMusicUrl() {  
  44.         return MusicUrl;  
  45.     }  
  46.   
  47.     public void setMusicUrl(String musicUrl) {  
  48.         MusicUrl = musicUrl;  
  49.     }  
  50.   
  51.     public String getHQMusicUrl() {  
  52.         return HQMusicUrl;  
  53.     }  
  54.   
  55.     public void setHQMusicUrl(String musicUrl) {  
  56.         HQMusicUrl = musicUrl;  
  57.     }  
  58.   
  59. }  

 
 構建好之后的項目結構圖為

 

 

到這里,請求消息和響應消息的實體類都定義好了

 

解析請求消息

 

用戶向微信公眾平台發送消息后,微信公眾平台會通過post請求發送給我們。

上一章中WeixinController 類的post方法我們空着

 

 現在我們要在這里處理用戶請求了。

 

因為微信的發送和接收都是用xml格式的,所以我們需要處理請求過來的xml格式。

發送的時候也需要轉化成xml格式再發送給微信,所以封裝了消息處理的工具類,用到dome4j和xstream兩個jar包

Java代碼   收藏代碼
  1. package com.ifp.weixin.util;  
  2.   
  3. import java.io.InputStream;  
  4. import java.io.Writer;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import javax.servlet.http.HttpServletRequest;  
  10.   
  11. import org.dom4j.Document;  
  12. import org.dom4j.Element;  
  13. import org.dom4j.io.SAXReader;  
  14.   
  15. import com.ifp.weixin.entity.Message.resp.Article;  
  16. import com.ifp.weixin.entity.Message.resp.MusicMessage;  
  17. import com.ifp.weixin.entity.Message.resp.NewsMessage;  
  18. import com.ifp.weixin.entity.Message.resp.TextMessage;  
  19. import com.thoughtworks.xstream.XStream;  
  20. import com.thoughtworks.xstream.core.util.QuickWriter;  
  21. import com.thoughtworks.xstream.io.HierarchicalStreamWriter;  
  22. import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;  
  23. import com.thoughtworks.xstream.io.xml.XppDriver;  
  24.   
  25. /** 
  26.  * 消息工具類 
  27.  *  
  28.  */  
  29. public class MessageUtil {  
  30.   
  31.     /** 
  32.      * 解析微信發來的請求(XML) 
  33.      *  
  34.      * @param request 
  35.      * @return 
  36.      * @throws Exception 
  37.      */  
  38.     public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {  
  39.         // 將解析結果存儲在HashMap中  
  40.         Map<String, String> map = new HashMap<String, String>();  
  41.   
  42.         // 從request中取得輸入流  
  43.         InputStream inputStream = request.getInputStream();  
  44.         // 讀取輸入流  
  45.         SAXReader reader = new SAXReader();  
  46.         Document document = reader.read(inputStream);  
  47.         // 得到xml根元素  
  48.         Element root = document.getRootElement();  
  49.         // 得到根元素的所有子節點  
  50.           
  51.         @SuppressWarnings("unchecked")  
  52.         List<Element> elementList = root.elements();  
  53.   
  54.         // 遍歷所有子節點  
  55.         for (Element e : elementList)  
  56.             map.put(e.getName(), e.getText());  
  57.   
  58.         // 釋放資源  
  59.         inputStream.close();  
  60.         inputStream = null;  
  61.   
  62.         return map;  
  63.     }  
  64.   
  65.     /** 
  66.      * 文本消息對象轉換成xml 
  67.      *  
  68.      * @param textMessage 文本消息對象 
  69.      * @return xml 
  70.      */  
  71.     public static String textMessageToXml(TextMessage textMessage) {  
  72.         xstream.alias("xml", textMessage.getClass());  
  73.         return xstream.toXML(textMessage);  
  74.     }  
  75.   
  76.     /** 
  77.      * 音樂消息對象轉換成xml 
  78.      *  
  79.      * @param musicMessage 音樂消息對象 
  80.      * @return xml 
  81.      */  
  82.     public static String musicMessageToXml(MusicMessage musicMessage) {  
  83.         xstream.alias("xml", musicMessage.getClass());  
  84.         return xstream.toXML(musicMessage);  
  85.     }  
  86.   
  87.     /** 
  88.      * 圖文消息對象轉換成xml 
  89.      *  
  90.      * @param newsMessage 圖文消息對象 
  91.      * @return xml 
  92.      */  
  93.     public static String newsMessageToXml(NewsMessage newsMessage) {  
  94.         xstream.alias("xml", newsMessage.getClass());  
  95.         xstream.alias("item", new Article().getClass());  
  96.         return xstream.toXML(newsMessage);  
  97.     }  
  98.   
  99.     /** 
  100.      * 擴展xstream,使其支持CDATA塊 
  101.      *  
  102.      */  
  103.     private static XStream xstream = new XStream(new XppDriver() {  
  104.         public HierarchicalStreamWriter createWriter(Writer out) {  
  105.             return new PrettyPrintWriter(out) {  
  106.                 // 對所有xml節點的轉換都增加CDATA標記  
  107.                 boolean cdata = true;  
  108.                 protected void writeText(QuickWriter writer, String text) {  
  109.                     if (cdata) {  
  110.                         writer.write("<![CDATA[");  
  111.                         writer.write(text);  
  112.                         writer.write("]]>");  
  113.                     } else {  
  114.                         writer.write(text);  
  115.                     }  
  116.                 }  
  117.             };  
  118.         }  
  119.     });  
  120.       
  121.       
  122. }  

 接下來在處理業務邏輯,建立一個接收並響應消息的service類,並針對用戶輸入的1或2回復不同的信息給用戶

 

Java代碼   收藏代碼
  1. package com.ifp.weixin.biz.core.impl;  
  2.   
  3. import java.util.Date;  
  4. import java.util.Map;  
  5.   
  6. import javax.servlet.http.HttpServletRequest;  
  7.   
  8. import org.apache.log4j.Logger;  
  9. import org.springframework.stereotype.Service;  
  10.   
  11. import com.ifp.weixin.biz.core.CoreService;  
  12. import com.ifp.weixin.constant.Constant;  
  13. import com.ifp.weixin.entity.Message.resp.TextMessage;  
  14. import com.ifp.weixin.util.MessageUtil;  
  15.   
  16. @Service("coreService")  
  17. public class CoreServiceImpl implements CoreService{  
  18.   
  19.     public static Logger log = Logger.getLogger(CoreServiceImpl.class);  
  20.       
  21.       
  22.     @Override  
  23.     public String processRequest(HttpServletRequest request) {  
  24.         String respMessage = null;  
  25.         try {  
  26.             // xml請求解析  
  27.             Map<String, String> requestMap = MessageUtil.parseXml(request);  
  28.   
  29.             // 發送方帳號(open_id)  
  30.             String fromUserName = requestMap.get("FromUserName");  
  31.             // 公眾帳號  
  32.             String toUserName = requestMap.get("ToUserName");  
  33.             // 消息類型  
  34.             String msgType = requestMap.get("MsgType");  
  35.   
  36.             TextMessage textMessage = new TextMessage();  
  37.             textMessage.setToUserName(fromUserName);  
  38.             textMessage.setFromUserName(toUserName);  
  39.             textMessage.setCreateTime(new Date().getTime());  
  40.             textMessage.setMsgType(Constant.RESP_MESSAGE_TYPE_TEXT);  
  41.             textMessage.setFuncFlag(0);  
  42.             // 文本消息  
  43.             if (msgType.equals(Constant.REQ_MESSAGE_TYPE_TEXT)) {  
  44.                 // 接收用戶發送的文本消息內容  
  45.                 String content = requestMap.get("Content");  
  46.   
  47.                 if ("1".equals(content)) {  
  48.                     textMessage.setContent("1是很好的");  
  49.                     // 將文本消息對象轉換成xml字符串  
  50.                     respMessage = MessageUtil.textMessageToXml(textMessage);  
  51.                 }else if ("2".equals(content)) {  
  52.                     textMessage.setContent("我不是2貨");  
  53.                     // 將文本消息對象轉換成xml字符串  
  54.                     respMessage = MessageUtil.textMessageToXml(textMessage);  
  55.                 }  
  56.             }   
  57.               
  58.               
  59.         } catch (Exception e) {  
  60.             e.printStackTrace();  
  61.         }  
  62.         return respMessage;  
  63.     }  
  64.   
  65.   
  66. }  

 接下來在controller里面的post方法里面調用即可

 

WeixinController類的完整代碼

Java代碼   收藏代碼
  1. package com.ifp.weixin.controller;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5. import java.io.UnsupportedEncodingException;  
  6.   
  7. import javax.annotation.Resource;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import org.springframework.stereotype.Controller;  
  12. import org.springframework.web.bind.annotation.RequestMapping;  
  13. import org.springframework.web.bind.annotation.RequestMethod;  
  14.   
  15. import com.ifp.weixin.biz.core.CoreService;  
  16. import com.ifp.weixin.util.SignUtil;  
  17.   
  18. @Controller  
  19. @RequestMapping("/weixinCore")  
  20. public class WeixinController {  
  21.   
  22.     @Resource(name="coreService")  
  23.     private CoreService coreService;  
  24.       
  25.     @RequestMapping(method = RequestMethod.GET)  
  26.     public void get(HttpServletRequest request, HttpServletResponse response) {  
  27.         // 微信加密簽名,signature結合了開發者填寫的token參數和請求中的timestamp參數、nonce參數。  
  28.         String signature = request.getParameter("signature");  
  29.         // 時間戳  
  30.         String timestamp = request.getParameter("timestamp");  
  31.         // 隨機數  
  32.         String nonce = request.getParameter("nonce");  
  33.         // 隨機字符串  
  34.         String echostr = request.getParameter("echostr");  
  35.   
  36.         PrintWriter out = null;  
  37.         try {  
  38.             out = response.getWriter();  
  39.             // 通過檢驗signature對請求進行校驗,若校驗成功則原樣返回echostr,否則接入失敗  
  40.             if (SignUtil.checkSignature(signature, timestamp, nonce)) {  
  41.                 out.print(echostr);  
  42.             }  
  43.         } catch (IOException e) {  
  44.             e.printStackTrace();  
  45.         } finally {  
  46.             out.close();  
  47.             out = null;  
  48.         }  
  49.     }  
  50.   
  51.     @RequestMapping(method = RequestMethod.POST)  
  52.     public void post(HttpServletRequest request, HttpServletResponse response) {  
  53.         try {  
  54.             request.setCharacterEncoding("UTF-8");  
  55.         } catch (UnsupportedEncodingException e) {  
  56.             e.printStackTrace();  
  57.         }  
  58.         response.setCharacterEncoding("UTF-8");  
  59.   
  60.         // 調用核心業務類接收消息、處理消息  
  61.         String respMessage = coreService.processRequest(request);  
  62.   
  63.         // 響應消息  
  64.         PrintWriter out = null;  
  65.         try {  
  66.             out = response.getWriter();  
  67.             out.print(respMessage);  
  68.         } catch (IOException e) {  
  69.             e.printStackTrace();  
  70.         } finally {  
  71.             out.close();  
  72.             out = null;  
  73.         }  
  74.     }  
  75.   
  76. }  

 

 效果如下:

 

 ok,大功告成,消息的接收和發送就寫完了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM