java 实现微信公众平台的接口
博客专区 > 陆朋 的博客 > 博客详情
java 实现微信公众平台的接口
陆朋 发表于4年前
java 实现微信公众平台的接口
  • 发表于 4年前
  • 阅读 398
  • 收藏 21
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

  1. <%@page  import="java.util.Date"%>

  2. <%@page  import="org.dom4j.Element"%>

  3. <%@page  import="org.dom4j.DocumentHelper"%>

  4. <%@page  import="org.dom4j.Document"%>

  5. <%@page  import="java.io.IOException"%>

  6. <%@page  import="java.io.InputStreamReader"%>

  7. <%@page  import="java.io.BufferedReader"%>

  8. <%@page  import="java.io.Reader"%>

  9. <%@page  import="java.security.MessageDigest"%>

  10. <%@page  import="java.util.Arrays"%>

  11. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

  12. <%

  13. //WeiXinHandler为内部类不能使用非final类型的对象

  14. final String TOKEN="weixin";

  15. final HttpServletRequest final_request=request;

  16. final HttpServletResponse final_response=response;

  17. %>

  18. <%

  19. class WeiXinHandler{

  20. public void valid(){

  21. String echostr=final_request.getParameter("echostr");

  22. if(null==echostr||echostr.isEmpty()){

  23. responseMsg();

  24. }else{

  25. if(this.checkSignature()){

  26. this.print(echostr);

  27. }else{

  28. this.print("error");

  29. }

  30. }

  31. }

  32. //自动回复内容

  33. public void responseMsg(){

  34. String postStr=null;

  35. try{

  36. postStr=this.readStreamParameter(final_request.getInputStream());

  37. }catch(Exception e){

  38. e.printStackTrace();

  39. }

  40. //System.out.println(postStr);

  41. if (null!=postStr&&!postStr.isEmpty()){

  42. Document document=null;

  43. try{

  44. document = DocumentHelper.parseText(postStr);

  45. }catch(Exception e){

  46. e.printStackTrace();

  47. }

  48. if(null==document){

  49. this.print("");

  50. return;

  51. }

  52. Element root=document.getRootElement();

  53. String fromUsername = root.elementText("FromUserName");

  54. String toUsername = root.elementText("ToUserName");

  55. String keyword = root.elementTextTrim("Content");

  56. String time = new Date().getTime()+"";

  57. String textTpl = "<xml>"+

  58. "<ToUserName><![CDATA[%1$s]]></ToUserName>"+

  59. "<FromUserName><![CDATA[%2$s]]></FromUserName>"+

  60. "<CreateTime>%3$s</CreateTime>"+

  61. "<MsgType><![CDATA[%4$s]]></MsgType>"+

  62. "<Content><![CDATA[%5$s]]></Content>"+

  63. "<FuncFlag>0</FuncFlag>"+

  64. "</xml>";

  65.  

  66. if(null!=keyword&&!keyword.equals(""))

  67. {

  68. String msgType = "text";

  69. String contentStr = "Welcome to wechat world!";

  70. String resultStr = textTpl.format(textTpl, fromUsername, toUsername, time, msgType, contentStr);

  71. this.print(resultStr);

  72. }else{

  73. this.print("Input something...");

  74. }

  75.  

  76. }else {

  77. this.print("");

  78. }

  79. }

  80. //微信接口验证

  81. public boolean checkSignature(){

  82. String signature = final_request.getParameter("signature");

  83. String timestamp = final_request.getParameter("timestamp");

  84. String nonce = final_request.getParameter("nonce");

  85. String token=TOKEN;

  86. String[] tmpArr={token,timestamp,nonce};

  87. Arrays.sort(tmpArr);

  88. String tmpStr=this.ArrayToString(tmpArr);

  89. tmpStr=this.SHA1Encode(tmpStr);

  90. if(tmpStr.equalsIgnoreCase(signature)){

  91. return true;

  92. }else{

  93. return false;

  94. }

  95. }

  96. //向请求端发送返回数据

  97. public void print(String content){

  98. try{

  99. final_response.getWriter().print(content);

  100. final_response.getWriter().flush();

  101. final_response.getWriter().close();

  102. }catch(Exception e){

  103.  

  104. }

  105. }

  106. //数组转字符串

  107. public String ArrayToString(String [] arr){

  108. StringBuffer bf = new StringBuffer();

  109. for(int i = 0; i < arr.length; i++){

  110. bf.append(arr[i]);

  111. }

  112. return bf.toString();

  113. }

  114. //sha1加密

  115. public String SHA1Encode(String sourceString) {

  116. String resultString = null;

  117. try {

  118. resultString = new String(sourceString);

  119. MessageDigest md = MessageDigest.getInstance("SHA-1");

  120. resultString = byte2hexString(md.digest(resultString.getBytes()));

  121. } catch (Exception ex) {

  122. }

  123. return resultString;

  124. }

  125. public final String byte2hexString(byte[] bytes) {

  126. StringBuffer buf = new StringBuffer(bytes.length * 2);

  127. for (int i = 0; i < bytes.length; i++) {

  128. if (((int) bytes[i] & 0xff) < 0x10) {

  129. buf.append("0");

  130. }

  131. buf.append(Long.toString((int) bytes[i] & 0xff, 16));

  132. }

  133. return buf.toString().toUpperCase();

  134. }

  135. //从输入流读取post参数

  136. public String readStreamParameter(ServletInputStream in){

  137. StringBuilder buffer = new StringBuilder();

  138. BufferedReader reader=null;

  139. try{

  140. reader = new BufferedReader(new InputStreamReader(in));

  141. String line=null;

  142. while((line = reader.readLine())!=null){

  143. buffer.append(line);

  144. }

  145. }catch(Exception e){

  146. e.printStackTrace();

  147. }finally{

  148. if(null!=reader){

  149. try {

  150. reader.close();

  151. } catch (IOException e) {

  152. e.printStackTrace();

  153. }

  154. }

  155. }

  156. return buffer.toString();

  157. }

  158. }

  159. %>

  160. <%

  161. WeiXinHandler handler=new WeiXinHandler();

  162. handler.valid();

  163. %>

共有 人打赏支持
粉丝 6
博文 29
码字总数 29916
×
陆朋
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: