最近要给公司写个微信的公众平台自动回复的程序,就研究了微信的公众平台的,发现目前api提供的功能还比较少,仅仅只能被动式应答,而且只能收取文字,图片和地理信息类的消息,但是从这个可以在线管理消息来看,其实是可以用Java模拟浏览器的行为从而实现高级的功能,比如获取声音,主动群发消息,但是由于这些功能不是api中提供的,存在以后接口发生变化的可能,所以可以用来做个小工具来使用这些高级功能但不适合在产品中使用

官方给的调用api的例子只有php的,我这里用java实现了个简单的文本回复的,DOM解析部分因为刚好有dom4j的jar包就用了,换成jdk自带的w3c的dom解析也行,反正消息xml内容很少

 

 

Java代码
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.PrintWriter;
  4. import java.security.MessageDigest;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.util.Arrays;
  7.  
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. import org.dom4j.Document;
  13. import org.dom4j.DocumentException;
  14. import org.dom4j.Element;
  15. import org.dom4j.io.SAXReader;
  16.  
  17. public class WeixinpublicServlet extends HttpServlet {
  18.  
  19. private static final long serialVersionUID = -4267408236898837036L;
  20.  
  21. private static final String TOKEN = "weixin";
  22.  
  23. private static final String RESPONSE_TXT = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>";
  24.  
  25. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  26. String signature = request.getParameter("signature");
  27. String timestamp = request.getParameter("timestamp");
  28. String nonce = request.getParameter("nonce");
  29.  
  30. PrintWriter out = response.getWriter();
  31. if(checkSignature(signature,timestamp,nonce)){
  32. out.print(request.getParameter("echostr"));
  33. }
  34. out.close();
  35. out = null;
  36. }
  37.  
  38. public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
  39. request.setCharacterEncoding("UTF-8");
  40. response.setCharacterEncoding("UTF-8");
  41. PrintWriter out = response.getWriter();
  42.  
  43. Document doc = null;
  44. SAXReader reader = new SAXReader();
  45. InputStream in = request.getInputStream();
  46. try {
  47. doc = reader.read(in);
  48. Element root = doc.getRootElement();
  49. String toUserName = root.element("ToUserName").getTextTrim();
  50. String fromUserName = root.element("FromUserName").getTextTrim();
  51. String content = root.element("Content").getTextTrim();
  52. // root.element("MsgType")可以获取消息类型
  53. // 这里只回复简单文本信息
  54. out.printf(RESPONSE_TXT, fromUserName,toUserName,System.currentTimeMillis(),"text","您发送的内容是:"+content);
  55. } catch (DocumentException e) {
  56. e.printStackTrace();
  57. }
  58. in.close();
  59. in = null;
  60. out.close();
  61. out = null;
  62. }
  63.  
  64. private static boolean checkSignature(String signature,String timestamp,String nonce){
  65. String[] arr = new String[]{TOKEN,timestamp,nonce};
  66. Arrays.sort(arr);
  67. StringBuilder content = new StringBuilder();
  68. for(int i=0;i<arr.length;i++){
  69. content.append(arr[i]);
  70. }
  71. MessageDigest md = null;
  72. String tmpStr = null;
  73.  
  74. try {
  75. md = MessageDigest.getInstance("SHA-1");
  76. byte[] digest = md.digest(content.toString().getBytes());
  77. tmpStr = byteToStr(digest);
  78. } catch (NoSuchAlgorithmException e) {
  79. e.printStackTrace();
  80. }
  81.  
  82. content = null;
  83. return tmpStr!=null?tmpStr.equals(signature.toUpperCase()):false;
  84. }
  85.  
  86. // 将字节转换为十六进制字符串
  87. private static String byteToHexStr(byte ib) {
  88. char[] Digit = {
  89. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
  90. 'D', 'E', 'F'
  91. };
  92. char[] ob = new char[2];
  93. ob[0] = Digit[(ib >>> 4) & 0X0F];
  94. ob[1] = Digit[ib & 0X0F];
  95.  
  96. String s = new String(ob);
  97. return s;
  98. }
  99.  
  100. // 将字节数组转换为十六进制字符串
  101. private static String byteToStr(byte[] bytearray) {
  102. String strDigest = "";
  103. for (int i = 0; i < bytearray.length; i++) {
  104. strDigest += byteToHexStr(bytearray[i]);
  105. }
  106. return strDigest;
  107. }
  108.  
  109. }