微信开发的消息接口

互联网 17-3-9
这篇文章一起看看微信开发的消息接口

感觉微信开发就是要调用微信的接口,所以在没安排工作的时候看和试着调用微信接口,调用微信接口需要发送http的get和post请求,所以最好先写个httputil类,专门发送get和post请求,然而我的java网络编程学的并不好,于是百度一些代码,然后自己封装一些,可以正常使用就行了,代码如下

import java.io.BufferedReader;  import java.io.DataInputStream;  import java.io.DataOutputStream;  import java.io.File;  import java.io.FileInputStream;  import java.io.IOException;  import java.io.InputStream;  import java.io.InputStreamReader;  import java.io.OutputStream;  import java.io.PrintWriter;  import java.net.HttpURLConnection;  import java.net.URL;  import java.net.URLConnection;  import java.util.Iterator;  import java.util.Map;  import javax.activation.MimetypesFileTypeMap;  /**   *    * @author luolei   *   */  public class HttpUtil {            public static String httpGet(String httpUrl){          StringBuffer buffer = null;           try{              URL url = new URL(httpUrl);              HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();                httpUrlConn.setDoInput(true);                httpUrlConn.setRequestMethod("GET");            // 获取输入流                InputStream inputStream = httpUrlConn.getInputStream();                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);                // 读取返回结果                buffer = new StringBuffer();                String str = null;                while ((str = bufferedReader.readLine()) != null) {                    buffer.append(str);                }                // 释放资源                bufferedReader.close();                inputStreamReader.close();                inputStream.close();                httpUrlConn.disconnect();           }catch(Exception e){              e.printStackTrace();          }          return buffer.toString();                }            /**       *        * 发 post 请求,       */      public static String httpPost(String httpUrl,String data){          PrintWriter out = null;          BufferedReader in = null;          String result = "";          try {              URL realUrl = new URL(httpUrl);              // 打开和URL之间的连接              URLConnection conn = realUrl.openConnection();              // 设置通用的请求属性              conn.setRequestProperty("accept", "*/*");              conn.setRequestProperty("connection", "Keep-Alive");              conn.setRequestProperty("user-agent",                      "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");              // 发送POST请求必须设置如下两行              conn.setDoOutput(true);              conn.setDoInput(true);              // 获取URLConnection对象对应的输出流              out = new PrintWriter(conn.getOutputStream());              // 发送请求参数              out.print(data);              // flush输出流的缓冲              out.flush();              // 定义BufferedReader输入流来读取URL的响应              in = new BufferedReader(                      new InputStreamReader(conn.getInputStream()));              String line;              while ((line = in.readLine()) != null) {                  result += line;              }          } catch (Exception e) {              System.out.println("发送 POST 请求出现异常!"+e);              e.printStackTrace();          }          //使用finally块来关闭输出流、输入流          finally{              try{                  if(out!=null){                      out.close();                  }                  if(in!=null){                      in.close();                  }              }              catch(IOException ex){                  ex.printStackTrace();              }          }          return result;      }            /**       * 上传图片       *        * @param urlStr       * @param textMap       * @param fileMap       * @return       */      public static String formUpload(String urlStr, Map<String, String> textMap,              Map<String, String> fileMap) {          String res = "";          HttpURLConnection conn = null;          String BOUNDARY = "---------------------------123821742118716"; //boundary就是request头和上传文件内容的分隔符          try {              URL url = new URL(urlStr);              conn = (HttpURLConnection) url.openConnection();  //            System.out.println(conn);              conn.setConnectTimeout(5000);              conn.setReadTimeout(30000);              conn.setDoOutput(true);              conn.setDoInput(true);              conn.setUseCaches(false);              conn.setRequestMethod("POST");              conn.setRequestProperty("Connection", "Keep-Alive");              conn                      .setRequestProperty("User-Agent",                              "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");              conn.setRequestProperty("Content-Type",                      "multipart/form-data; boundary=" + BOUNDARY);              OutputStream out = new DataOutputStream(conn.getOutputStream());              // text              if (textMap != null) {                  StringBuffer strBuf = new StringBuffer();                  Iterator iter = textMap.entrySet().iterator();                  while (iter.hasNext()) {                      Map.Entry entry = (Map.Entry) iter.next();                      String inputName = (String) entry.getKey();                      String inputValue = (String) entry.getValue();                      if (inputValue == null) {                          continue;                      }                      strBuf.append("\r\n").append("--").append(BOUNDARY).append(                              "\r\n");                      strBuf.append("Content-Disposition: form-data; name=\""                              + inputName + "\"\r\n\r\n");                      strBuf.append(inputValue);                  }                  out.write(strBuf.toString().getBytes());              }              // file              if (fileMap != null) {                  Iterator iter = fileMap.entrySet().iterator();                  while (iter.hasNext()) {                      Map.Entry entry = (Map.Entry) iter.next();                      String inputName = (String) entry.getKey();                      String inputValue = (String) entry.getValue();                      if (inputValue == null) {                          continue;                      }                      File file = new File(inputValue);                      String filename = file.getName();                      String contentType = new MimetypesFileTypeMap()                              .getContentType(file);                      if (filename.endsWith(".png")) {                          contentType = "image/png";                      }                      if (contentType == null || contentType.equals("")) {                          contentType = "application/octet-stream";                      }                      StringBuffer strBuf = new StringBuffer();                      strBuf.append("\r\n").append("--").append(BOUNDARY).append(                              "\r\n");                      strBuf.append("Content-Disposition: form-data; name=\""                              + inputName + "\"; filename=\"" + filename                              + "\"\r\n");                      strBuf.append("Content-Type:" + contentType + "\r\n\r\n");                      out.write(strBuf.toString().getBytes());                      DataInputStream in = new DataInputStream(                              new FileInputStream(file));                      int bytes = 0;                      byte[] bufferOut = new byte[1024];                      while ((bytes = in.read(bufferOut)) != -1) {                          out.write(bufferOut, 0, bytes);                      }                      in.close();                  }              }              byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();              out.write(endData);              out.flush();              out.close();              // 读取返回数据              StringBuffer strBuf = new StringBuffer();              BufferedReader reader = new BufferedReader(new InputStreamReader(                      conn.getInputStream()));              String line = null;              while ((line = reader.readLine()) != null) {                  strBuf.append(line).append("\n");              }              res = strBuf.toString();              reader.close();              reader = null;          } catch (Exception e) {              System.out.println("发送POST请求出错。" + urlStr);              e.printStackTrace();          } finally {              if (conn != null) {                  conn.disconnect();                  conn = null;              }          }          return res;      }        }

其中的httpGet 和httpPost 用来发送get,和post请求,微信开发里面,消息接口一般是xml格式的,其他的接口上传和返回的数据一般是json,所以需要一个解析json的包,我用的是fastjson,当然也可以用gson

现在开始消息接口的测试,首先要了解请求过程:

微信服务器会根据填写的url发送get请求进行验证,当验证成功,还是根据url发送post请求,消息格式为xml格式

消息类型开发文档上有,主要有文本,图片,语音等消息,还有一些事件,如关注,点击,和跳转。

这些消息和事件是xml格式,所以要对xml格式的消息进行解析,我用的dom4j解析,

在之前验证接入的servlet的doPost方法解析消息,

我是按照柳峰的博客里面写的方法,写了个MessageUtil,里面封装了解析xml的方法,并把解析出来的结果放在map<string,string>中,具体代码如下:

public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {            // 将解析结果存储在HashMap中            Map<String, String> map = new HashMap<String, String>();                // 从request中取得输入流            InputStream inputStream = request.getInputStream();            // 读取输入流            SAXReader reader = new SAXReader();            Document document = reader.read(inputStream);            // 得到xml根元素            Element root = document.getRootElement();            // 得到根元素的所有子节点            List<Element> elementList = root.elements();                // 遍历所有子节点            for (Element e : elementList)                map.put(e.getName(), e.getText());                // 释放资源            inputStream.close();            inputStream = null;                return map;        }

那么经过解析后的xml会按照 标签名 - 内容 保存在map中

然后可以从中取出消息类型msgType

String msgType = requestMap.get("MsgType");

然后判断消息的类型,不同的消息类型,让不同的servlet去处理,

// 文本消息              if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {                  request.getRequestDispatcher("TextMessage").forward(request, response);              }              // 图片消息              else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {                  request.getRequestDispatcher("ImageServlet").forward(request, response);              }              // 地理位置消息              else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {                  request.getRequestDispatcher("LocationServlet").forward(request, response);              }              // 链接消息              else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {                  request.getRequestDispatcher("LinkServlet").forward(request, response);              }              // 音频消息              else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {                  request.getRequestDispatcher("VedioServlet").forward(request, response);              }              // 事件推送              else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {                  // 事件类型                  String eventType = requestMap.get("Event");                  // 订阅                  if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {                      request.getRequestDispatcher("SubServlet").forward(request, response);                  }                  // 取消订阅                  else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {                      // TODO 取消订阅后用户再收不到公众号发送的消息,因此不需要回复消息                                        }                  // 自定义菜单点击事件                  else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {                      // TODO 自定义菜单权没有开放,暂不处理该类消息                      request.getRequestDispatcher("ClickServlet").forward(request, response);                  }              }

不同的servlet里面处理不同的消息,可以根据需要返回不同的消息,返回消息的格式也是xml格式的,返回消息类型跟接受的消息类型基本类似,可以对这些返回的消息进行封装,每个xml标签对应字段名,内容就是字段的内容

例子:

public class BaseMessageResp {      // 接收方帐号(收到的OpenID)        private String ToUserName;        // 开发者微信号        private String FromUserName;        // 消息创建时间 (整型)        private long CreateTime;        // 消息类型(text/music/news)        private String MsgType;        // 位0x0001被标志时,星标刚收到的消息        private int FuncFlag;

省略了set,get方法

public class TextMessage extends BaseMessageResp {      // 回复的消息内容        private String Content;            public String getContent() {            return Content;        }            public void setContent(String content) {            Content = content;        }  }

因为不同的消息有相同的字段,因此写了通用的基类。

现在离返回消息给用户还差一步,技术将这些pojo类转化为xml字符串

用的是xstream

/**        * 文本消息对象转换成xml        *         * @param textMessage 文本消息对象        * @return xml        */        public static String textMessageToXml(TextMessage textMessage) {            xstream.alias("xml", textMessage.getClass());            return xstream.toXML(textMessage);        }

这里只是简单的描述,具体的可以以看柳峰的博客,链接我忘记了,应该可以百度的到

最后将得到的string 返回给微信服务器就可以回复用户了。

只用这些消息接口就可以写一个简单的订阅号了,应该,一般公司的公众号好像是通过view类型的button跳到自己的网站里面去。

现在用上面的接口可以接受用户发送的各种消息,然后提前消息,可以自己进行处理,或者调用一些api,如天气,笑话,文章等api,得到结果,解析后,按照自己希望的格式返回给用户,可以实习一个生活助手之类的订阅号,但是个人申请的订阅号的权限有限,不知道能不能够胜任。

以上就是微信开发的消息接口的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: 微信开发
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:微信开发之解绑设备通知的方法

相关资讯