《2022年微信公众平台接口调用 .pdf》由会员分享,可在线阅读,更多相关《2022年微信公众平台接口调用 .pdf(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、首先是一个servlet:package com.baitw; import java.io.BufferedReader; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.serv
2、let.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.baitw.dao.NodeDao; import com.baitw.dao.impl.NodeDaoImpl; import com.baitw.hibernate.entity.TChannelnode; public class WeixinServlet extends HttpServlet private static final
3、long serialV ersionUID = 1L; public static final String Token = token; /* * 用来接收微信公众平台的验证*/ Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException String signature = request.getParameter(signature);String timestamp = request.getPa
4、rameter(timestamp); String nonce = request.getParameter(nonce); String ArrTmp = Token, timestamp, nonce ; Arrays.sort(ArrTmp); StringBuffer sb = new StringBuffer(); for (int i = 0; i ArrTmp.length; i+) sb.append(ArrTmpi); String pwd = Encrypt(sb.toString(); String echostr = request.getParameter(echo
5、str); System.out.println(pwd=+pwd); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 7 页 - - - - - - - - - System.out.println(echostr=+echostr); if(pwd.equals(signature) if(!.equals(echostr) & echostr != null) response.getWriter().print(echostr); /* * 用来向普通用户传送信息
6、*/ Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException StringBuffer sb = new StringBuffer(); String line; Map map = null; NodeDao nd = new NodeDaoImpl(); List list = null; try BufferedReader reader = request.getReader(); while
7、 (line = reader.readLine() != null) sb.append(line); map = XmlUtil.xml2Map(new String(sb.toString().getBytes(),UTF-8); catch (Exception e) e.printStackTrace(); sb = new StringBuffer(); if(isChinese(map.get(xml.Content) list = nd.getAllChannelnodes(map.get(xml.Content); if(list.size() = 0) sb.append(
8、) .append(map.get(xml.CreateTime).append() .append() .append(0); else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 7 页 - - - - - - - - - sb.append() .append(map.get(xml.CreateTime).append() .append(!CDA TA); for(int i=0;i) .append(0); else sb.append() .append
9、(map.get(xml.CreateTime) .append() .append() .append(0); response.setCharacterEncoding(UTF-8); response.getWriter().print(sb.toString(); Override public void destroy() super.destroy(); Override public void init() throws ServletException 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理
10、 - - - - - - - 第 3 页,共 7 页 - - - - - - - - - super.init(); public String Encrypt(String strSrc) MessageDigest md = null; String strDes = null; byte bt = strSrc.getBytes(); try md = MessageDigest.getInstance(SHA-1); md.update(bt); strDes = bytes2Hex(md.digest(); /to HexString catch (NoSuchAlgorithmEx
11、ception e) System.out.println(Invalid algorithm.); return null; return strDes; public String bytes2Hex(byte bts) String des = ; String tmp = null; for (int i = 0; i bts.length; i+) tmp = (Integer.toHexString(btsi & 0 xFF); if (tmp.length() = 1) des += 0; des += tmp; return des; public boolean isChin
12、ese(String str) boolean result=false; for (int i = 0; i =19968&chr1=171941)/汉字范围u4e00-u9fa5 (中文 ) result = true; return result; /*public static void main(String args) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 7 页 - - - - - - - - - StringBuffer sb = new Str
13、ingBuffer(); NodeDao nd = new NodeDaoImpl(); List list = nd.getAllChannelnodes(1); System.out.println(list.size(); for(TChannelnode node:list) sb.append(名称: ).append(node.getName().append( ,) .append(地址: ).append(node.getAddress().append(n); sb.append() .append() .append() .append(0); WeixinServlet
14、ws = new WeixinServlet(); System.out.println(ws.isChinese(1); System.out.println(sb.toString(); */ 该 servlet 中用到的map工具类如下:package com.baitw; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.util.HashMap; import java.util
15、.Iterator; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; public class XmlUtil 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - -
16、 - - - - 名师精心整理 - - - - - - - 第 5 页,共 7 页 - - - - - - - - - public static Map xml2Map(String xmlStr) throws JDOMException, IOException Map rtnMap = new HashMap(); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new StringReader(xmlStr); / 得到根节点Element root = doc.getRootElement();
17、 String rootName = root.getName(); rtnMap.put(root.name, rootName); / 调用递归函数,得到所有最底层元素的名称和值,加入map 中convert(root, rtnMap, rootName); return rtnMap; /* * 递归函数,找出最下层的节点并加入到map中,由 xml2Map 方法调用。* * param e * xml 节点,包括根节点* param map * 目标 map * param lastname * 从根节点到上一级节点名称连接的字串*/ public static void conver
18、t(Element e, Map map, String lastname) if (e.getAttributes().size() 0) Iterator it_attr = e.getAttributes().iterator(); while (it_attr.hasNext() Attribute attribute = (Attribute) it_attr.next(); String attrname = attribute.getName(); String attrvalue = e.getAttributeV alue(attrname); map.put(lastnam
19、e + . + attrname, attrvalue); List children = e.getChildren(); Iterator it = children.iterator(); while (it.hasNext() Element child = (Element) it.next(); String name = lastname + . + child.getName();/ 如果有子节点,则递归调用if (child.getChildren().size() 0) convert(child, map, name); else 名师资料总结 - - -精品资料欢迎下载
20、 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 7 页 - - - - - - - - - / 如果没有子节点,则把值加入map map.put(name, child.getText(); / 如果该节点有属性,则把所有的属性值也加入map if (child.getAttributes().size() 0) Iterator attr = child.getAttributes().iterator(); while (attr.hasNext() Attribute attribute = (Attribute) attr.next(); String attrname = attribute.getName(); String attrvalue = child.getAttributeV alue(attrname);map.put(name + . + attrname, attrvalue); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 7 页 - - - - - - - - -