微信公众平台深度开发JAVA版第一季 15.响应被动消息3
微信开发难度不大,但是逻辑关系很强.
这节课主要讲微信接口,至于在J2EE平台怎么把程序构造的更好不在这节课里面讨论.
package net.wxinterface;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.dom4j.DocumentException;import wx.in.In_Image;
import wx.in.In_Text;
import wx.in.In_Util;
import wx.in.In_Voice;
import wx.out.Out_Image;
import wx.out.Out_Text;
import wx.out.Out_Voice;import com.imooc.po.TextMessage;
import com.imooc.util.MessageUtil;
public class WX_Interface extends HttpServlet {/*** Constructor of the object.*/public WX_Interface() {super();}/*** Destruction of the servlet. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/*** The doGet method of the servlet. <br>** This method is called when a form has its tag value method equals to get.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//�ż���ǩ��,signature����˿�������д��tocken����������е�timestamp����nonce����String signature = request.getParameter("signature");//ʱ���String timestamp = request.getParameter("timestamp");//����� String nonce = request.getParameter("nonce");String echostr = request.getParameter("echostr");String tocken = "test";try{if(null != signature){String[] ArrTmp = {tocken,timestamp,nonce}; Arrays.sort(ArrTmp);StringBuffer sb = new StringBuffer();for(int i=0;i<ArrTmp.length;i++){sb.append(ArrTmp[i]);}MessageDigest md = MessageDigest.getInstance("SHA-1");byte[] bytes = md.digest(new String(sb).getBytes());StringBuffer buf = new StringBuffer();for(int i=0;i<bytes.length;i++){if(((int)bytes[i] & 0xff)<0x10){buf.append("0");}buf.append(Long.toString((int) bytes[i] & 0xff,16));}if(signature.equals(buf.toString())){response.getOutputStream().println(echostr);}}}catch(Exception e){e.printStackTrace();}System.out.println("test0");System.out.println("doGet");System.out.println("signature "+signature);System.out.println("timstamp "+timestamp);System.out.println("nonce "+nonce);System.out.println("echostr "+echostr);System.out.println("doGet");}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/*request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
try {Map<String,String> map = MessageUtil.xmlToMap(request);String fromUserName = map.get("FromUserName");String toUserName = map.get("ToUserName");String msgType = map.get("MsgType");String content = map.get("Content");//String message = null;//if("text".equals(msgType)){if(MessageUtil.MESSAGE_TEXT.equals(msgType)){if("1".equals(content)){message = MessageUtil.initText(toUserName,fromUserName,MessageUtil.firstMenu()); }else if("3".equals(content)){//message = MessageUtil.initText(toUserName,fromUserName,content); //�Ϳ��������ﰴ3����ͼƬ��Ϣ,���ͼƬ��Ϣ�Ĵ����Լ��ظ��Ѿ�д����message = MessageUtil.initImageMessage(toUserName,fromUserName);}else if("4".equals(content)){//��servlet����һ���ж�,������4��ʱ��ظ�һ�����ָ��� message = MessageUtil.initMusicMessage(toUserName,fromUserName);}else if("2".equals(content)){//message = MessageUtil.initText(toUserName,fromUserName,MessageUtil.secondMenu()); //�����ͼ����Ϣ�Ĵ����Լ��ظ��Ѿ�д���� message= MessageUtil.initNewsMessage(toUserName,fromUserName);}else if("?".equals(content)||" ? ".equals(content)){message = MessageUtil.initText(toUserName,fromUserName,MessageUtil.menuText());}TextMessage text = new TextMessage();text.setFromUserName(toUserName);//˭������,��ͷ���˭text.setToUserName(fromUserName);text.setMsgType("text");text.setCreateTime(new Date().getTime());text.setContent("���͵���Ϣ��:"+content);message = MessageUtil.textMessageToXml(text);System.out.println(message);}else if(MessageUtil.MESSAGE_EVENT.equals(msgType)){//�����Ϣ���͵���String eventType = map.get("Event");if(MessageUtil.MESSAGE_SUBSCRIBE.equals(eventType)){//��Ϣ�����¼�������message = MessageUtil.initText(toUserName, fromUserName, content);//��ע�Ź��ں�֮���ź�̨����}//�������Servlet�������һ�������ж�else if(MessageUtil.MESSAGE_CLICK.equals(eventType)){//�����������Click�˵���һ���¼�����//Click���͵���Ϣ����,���Event��Click���͵Ļ�,��ô����ظ�һ�����˵�message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.menuText());}else if(MessageUtil.MESSAGE_VIEW.equals(eventType)){//�����View���Ͳ˵�,�ͻظ�һ�������õ�UrlString url = map.get("EventKey");//�����View���Ͳ˵��Ļ�,���EventKey�������Url��ֵif("11".equals(url)){//���Keyֵ��11message = MessageUtil.initText(toUserName, fromUserName, url);}}else if(MessageUtil.MESSAGE_SCANCODE.equals(eventType)){//�ظ�һ�����ɨ���¼�String key = map.get("EventKey");//�����Click���Ͳ˵��Ļ�,EventKey���������õ�Keyֵmessage = MessageUtil.initText(toUserName, fromUserName, key);}}out .print(message);
} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();
}finally{out.close();
}System.out.println("doPost");*///response.setContentType("text/html");//PrintWriter out = response.getWriter();/* StringBuffer strb = new StringBuffer();ServletInputStream in = request.getInputStream();BufferedReader breader= new BufferedReader( new InputStreamReader(in,"UTF-8"));String str = null;while(null!=(str=breader.readLine())){strb.append(str);}//out.println(str);System.out.println(strb);*///out.flush();//out.close();
/*response.setContentType("text/html");
PrintWriter out = response.getWriter();StringBuffer strb = new StringBuffer();
//strb要存储一下request获取过来的东西
ServletInputStream in = request.getInputStream();BufferedReader breader = new BufferedReader( new InputStreamReader(in,"UTF-8"));//接收是对的,已经进行转码了.
String str = null;
//+-*\/\是运算符,赋值运算符=也是运算符/*
while(null!=(str = breader.readLine())){strb.append(str);
}
out.println(strb);
System.out.println(strb);
out.flush();
out.close();*/response.setContentType("text/html;charset=utf-8");String requestStr = In_Util.getStringFromRequest(request);System.out.println(requestStr);System.out.println("北风网");
/* In_Text in_Text = new In_Text(requestStr);System.out.println(in_Text);in_Text.setContent(in_Text.getContent()+ " 吃屎啦");*//* In_Image in_Image = new In_Image(requestStr);System.out.println(in_Image);*///Out_Text out_Text = new Out_Text(in_Text);//Out_Text out_Text = new Out_Text(in_Text,in_Text.getContent()+ " 吃屎啦");
/* Out_Text out_Text = new Out_Text(in_Text);out_Text.setContent(in_Text.getContent());*///System.out.println(out_Text.getStr("这是北风网。。。。。。。"));//response.getOutputStream().print(out_Text.getStr(in_Text.getContent()+ " 吃屎啦"));//response.getWriter().print(out_Text.getStr(in_Text.getContent()+ " 吃屎啦"));//response.getWriter().print(out_Text.getStr());//new BufferedWriter(new OutputStreamWriter(response.getOutputStream(),"UTF-8")).write(out_Text.getStr(in_Text.getContent()+ " chishila"));//response.getOutputStream().print(out_Text.getStr("这是北风网。。。。。。。"));//System.out.println(out_Text.getStr(in_Text.getContent()+ " 吃屎啦"));//System.out.println(out_Text.getStr());/*In_Image in_Image = new In_Image(requestStr);System.out.println(in_Image.getMediaId());Out_Image out_Image = new Out_Image(in_Image);out_Image.setMediaId(in_Image.getMediaId());response.getWriter().print(out_Image.getStr());*/
/* String shanghai = "szNaZ9h5SbG4kMDlCexVMvNLuezqTB20QE9XOgpbK5plhmCOnaO6JG6-ISXsIdvn";String xxx= "T9Bi3d-qxYbZnE6f1CsYJ2EfNrWDdr9x3Kpas_WUo04tc9b4zwTq9KZs1vQ6taQB";In_Text in_Text = new In_Text(requestStr);Out_Image out_Image = new Out_Image(in_Text);if("1".equals(in_Text.getContent())){out_Image.setMediaId(shanghai);}else{out_Image.setMediaId(xxx);}response.getWriter().print(out_Image.getStr());*/In_Voice in_Voice = new In_Voice(requestStr);Out_Voice out_Voice = new Out_Voice(in_Voice);out_Voice.setMediaId(in_Voice.getMediaId());response.getWriter().print(out_Voice.getStr());}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}}
package wx.out;import java.util.Date;import wx.in.In;public class Out_Voice {private String ToUserName =null;// 是 接收方帐号(收到的OpenID)private String FromUserName = null;//是 开发者微信号private String CreateTime = null;// 是 消息创建时间戳 (整型)private String MsgType = null;// 是 语音,voiceprivate String MediaId = null;// 是 通过素材管理中的接口上传多媒体文件,得到的idStringBuffer strb = new StringBuffer();StringBuffer strb_ = new StringBuffer();public Out_Voice(In in) {super();Date date = new Date();strb.append(" <ToUserName><![CDATA["+in.getFromUserName()+"]]></ToUserName> ");strb.append(" <FromUserName><![CDATA["+in.getToUserName()+"]]></FromUserName> ");strb.append(" <CreateTime>"+date.getTime()+"</CreateTime> ");strb.append(" <MsgType><![CDATA[voice]]></MsgType> ");}//public String getStr(String str){public String getStr(){strb_.append(" <xml> "); strb_.append(strb);strb_.append(" <Voice> ");strb_.append(" <MediaId><![CDATA["+this.getMediaId()+"]]></MediaId> ");strb_.append(" </Voice> ");strb_.append(" </xml> ");return strb_.toString();}public String getToUserName() {return ToUserName;}public void setToUserName(String toUserName) {ToUserName = toUserName;}public String getFromUserName() {return FromUserName;}public void setFromUserName(String fromUserName) {FromUserName = fromUserName;}public String getCreateTime() {return CreateTime;}public void setCreateTime(String createTime) {CreateTime = createTime;}public String getMsgType() {return MsgType;}public void setMsgType(String msgType) {MsgType = msgType;}public String getMediaId() {return MediaId;}public void setMediaId(String mediaId) {MediaId = mediaId;}}
package wx.out;import java.util.Date;import wx.in.In;public class Out_Image {private String ToUserName = null;// 是 接收方帐号(收到的OpenID)private String FromUserName = null;// 是 开发者微信号private String CreateTime =null;// 是 消息创建时间 (整型)private String MsgType =null;// 是 imageprivate String MediaId =null;// 是 通过素材管理中的接口上传多媒体文件,得到的id。StringBuffer strb = new StringBuffer();StringBuffer strb_ = new StringBuffer();public Out_Image(In in) {super();Date date = new Date();strb.append(" <ToUserName><![CDATA["+in.getFromUserName()+"]]></ToUserName> ");strb.append(" <FromUserName><![CDATA["+in.getToUserName()+"]]></FromUserName> ");strb.append(" <CreateTime>"+date.getTime()+"</CreateTime> ");strb.append(" <MsgType><![CDATA[Image]]></MsgType> ");}//public String getStr(String str){public String getStr(){strb_.append(" <xml> "); strb_.append(strb);strb_.append(" <Image> ");strb_.append(" <MediaId><![CDATA["+this.getMediaId()+"]]></MediaId> ");strb_.append(" </Image> ");strb_.append(" </xml> ");return strb_.toString();}public String getToUserName() {return ToUserName;}public void setToUserName(String toUserName) {ToUserName = toUserName;}public String getFromUserName() {return FromUserName;}public void setFromUserName(String fromUserName) {FromUserName = fromUserName;}public String getCreateTime() {return CreateTime;}public void setCreateTime(String createTime) {CreateTime = createTime;}public String getMsgType() {return MsgType;}public void setMsgType(String msgType) {MsgType = msgType;}public String getMediaId() {return MediaId;}public void setMediaId(String mediaId) {MediaId = mediaId;}}
package wx.out;import java.util.Date;import wx.in.In;public class Out_Text {private String ToUserName = null;// 是 接收方帐号(收到的OpenID)private String FromUserName = null;// 是 开发者微信号private String CreateTime = null;// 是 消息创建时间 (整型)private String MsgType =null;// 是 textprivate String Content = null;// 是 回复的消息内容(换行:在content中能够换行,微信客户端就支持换行显示)StringBuffer strb = new StringBuffer();StringBuffer strb_ = new StringBuffer();public Out_Text(In in) {//public Out_Text(In in,String str) {super();// TODO Auto-generated constructor stubDate date = new Date();strb.append(" <ToUserName><![CDATA["+in.getFromUserName()+"]]></ToUserName> ");strb.append(" <FromUserName><![CDATA["+in.getToUserName()+"]]></FromUserName> ");strb.append(" <CreateTime>"+date.getTime()+"</CreateTime> ");strb.append(" <MsgType><![CDATA[text]]></MsgType> ");//this.setContent(str);}//public String getStr(String str){public String getStr(){strb_.append(" <xml> "); strb_.append(strb);//strb_.append(" <Content><![CDATA["+str+"]]></Content> ");strb_.append(" <Content><![CDATA["+this.getContent()+"]]></Content> ");strb_.append(" </xml> ");return strb_.toString();}public String getToUserName() {return ToUserName;}public void setToUserName(String toUserName) {ToUserName = toUserName;}public String getFromUserName() {return FromUserName;}public void setFromUserName(String fromUserName) {FromUserName = fromUserName;}public String getCreateTime() {return CreateTime;}public void setCreateTime(String createTime) {CreateTime = createTime;}public String getMsgType() {return MsgType;}public void setMsgType(String msgType) {MsgType = msgType;}public String getContent() {return Content;}public void setContent(String content) {Content = content;}}
转载于:https://my.oschina.net/u/3206447/blog/900116
微信公众平台深度开发JAVA版第一季 15.响应被动消息3相关推荐
- 微信公众平台深度开发JAVA版第一季 14.响应被动消息2
好的程序是重构出来的.要recode7次. package net.wxinterface; import java.io.BufferedReader; import java.io.Buffere ...
- 微信公众平台深度开发JAVA版第一季 16.响应被动消息4
回复视频消息不成功,不知道为什么.是因为视频上传不成功吗? package wx.out;import java.util.Date; import java.util.List;import wx. ...
- 微信公众平台深度开发JAVA版第一季 08.接收普通消息1
上一节讲了开发的接口接入这一部分,这是非常重要的. 实现业务的逻辑应该写在POST方法这一块. GET是消息验证用的,验证接口那一块的. XML数据包:腾讯服务器传过来的数据是XML格式的传到你的业务 ...
- 微信公众平台深度开发JAVA版第一季 10.接收普通消息3
获取到从腾讯服务器过来的字符串,业务服务器可以拼接出来可以打印出来.后续的工作是把这个字符串封装成一个Bean对象. <xml><ToUserName><![CDATA[ ...
- 微信公众平台深度开发JAVA版第一季 30.菜单6
{"button":[{ "type": "click", "name": "说明001", &qu ...
- 微信公众平台深度开发JAVA版第一季 22.微信猜数字活动4
select count(*) from huo t where t.shuzi='0' 这个SQL语句查不查询得到记录取决于两个条件:第一个是数据库里面有没有这个数字,第二个是getCountByS ...
- 微信公众平台深度开发JAVA版第一季 27.菜单3
把字符串转化成对象. {"access_token":"zTZFxLrhM1vHAjXmq1ymk1JT8nlOKja4urFIr9PWqLYvFH9ZbWo8R6BmQ ...
- 视频教程-微信公众平台深度开发v2.0第3季——二维码、模板消息-微信开发
微信公众平台深度开发v2.0第3季--二维码.模板消息 微信企业号星级会员.10多年软件从业经历,国家级软件项目负责人,主要从事软件研发.软件企业员工技能培训.已经取得计算机技术与软件资格考试(软考) ...
- 视频教程-微信公众平台深度开发v2.0第6季——微信WEB开发、手机WEB技术-微信开发
微信公众平台深度开发v2.0第6季--微信WEB开发.手机WEB技术 微信企业号星级会员.10多年软件从业经历,国家级软件项目负责人,主要从事软件研发.软件企业员工技能培训.已经取得计算机技术与软件资 ...
最新文章
- php 许愿墙 阶段案例_房地产全周期设计管控流程解析及跨部门合作及案例分析...
- 表情转html,PHP转换emoji表情为HTML字符实体
- 从零开始配置MySQL MMM
- 瑞斯康达nms_瑞斯康达iTN产品资料
- 图像工程CH5:图像消噪和恢复
- Chapter 5 Blood Type——24
- phpcms父级调用二级子栏目名称和二级栏目文章 - 代码篇
- 微信开发者配置服务器信息,【开发】微信验证开发者接口配置信息,服务器没有正确响应Token....
- python sql注入脚本_python辅助sql手工注入猜解数据库案例分析
- SurfaceGo使用体验(使用半年后)
- vue baidu map之获取选中点的经纬度
- 算法导论9:栈的链表实现 2016.1.9
- Ruby module里的self
- Spring源码之创建代理
- 哈理工OJ 1926 函数式计算
- 2022年使用的最佳Javascript库
- 安卓Android类原生系统官网集合
- 非常好用的桌面壁纸更换软件
- (附源码)计算机毕业设计SSM旅游推广系统
- 微信小程序聊天室 前后端源码附效果图和数据库结构图