微信公众平台深度开发JAVA版第一季 12.接收普通消息5

181758_lcbW_3206447.png

182212_J4ul_3206447.png

import wx.in.In;
import wx.in.In_Image;
import wx.in.In_Text;
import wx.in.In_Util;

@SuppressWarnings("unused")
public class Test {
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
	     //text
	     String str = "<xml><ToUserName><![CDATA[gh_44e4d096da50]]></ToUserName><FromUserName><![CDATA[o2d2nwwoWhw1P2VDWiQHj1brqSc4]]></FromUserName><CreateTime>1495014482</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[北风网]]></Content><MsgId>6421038307652509766</MsgId></xml>";
	     
	     //图
	     
/*	     System.out.println(In_Util.getStrByXML(str,"ToUserName"));
	     System.out.println(In_Util.getStrByXML(str,"FromUserName"));*/
/*	     In_Text in_Text = new In_Text(str);
	     System.out.println(in_Text);*/
	     In in = new In(str);
	     System.out.println(in);
      }
}
package wx.in;

public class In {

	private String ToUserName = null;//开发者微信号
	private String FromUserName = null;// 发送方账号(一个OpenID)
	private String CreateTime = null;// 消息创建时间(整型)
	private String MsgType = null;//  text
	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 In() {
		super();
		// TODO Auto-generated constructor stub
	}
	public In(String requestStr) {
		this.setToUserName(In_Util.getStrByXML(requestStr, "ToUserName"));
		this.setFromUserName(In_Util.getStrByXML(requestStr, "FromUserName"));
		this.setCreateTime(In_Util.getStrByXML(requestStr, "CreateTime"));
		this.setMsgType(In_Util.getStrByXML(requestStr, "MsgType"));
		
	}
	@Override
	public String toString() {
		return "In [ToUserName=" + ToUserName + ", FromUserName="
				+ FromUserName + ", CreateTime=" + CreateTime + ", MsgType="
				+ MsgType + "]";
	}
	
 
}
package wx.in;

public class In_Image {
	private String ToUserName =null;//	开发者微信号
	private String FromUserName	=null;//发送方帐号(一个OpenID)
	private String CreateTime =null;	//消息创建时间 (整型)
	private String MsgType=null;	//image
	private String PicUrl	=null;//图片链接(由系统生成)
	private String MediaId	=null;//图片消息媒体id,可以调用多媒体文件下载接口拉取数据。
	private String MsgId	=null;//消息id,64位整型
	
	public In_Image() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public In_Image(String requestStr) {
		super();
		this.setToUserName(In_Util.getStrByXML(requestStr,"ToUserName"));
		this.setFromUserName(In_Util.getStrByXML(requestStr,"FromUserName"));
		this.setCreateTime(In_Util.getStrByXML(requestStr,"CreateTime"));
		this.setMsgType(In_Util.getStrByXML(requestStr,"MsgType"));
		this.setPicUrl(In_Util.getStrByXML(requestStr,"PicUrl"));
		this.setMediaId(In_Util.getStrByXML(requestStr,"MediaId"));
		this.setMsgId(In_Util.getStrByXML(requestStr,"MsgId"));
	}

	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 getPicUrl() {
		return PicUrl;
	}
	public void setPicUrl(String picUrl) {
		PicUrl = picUrl;
	}
	public String getMediaId() {
		return MediaId;
	}
	public void setMediaId(String mediaId) {
		MediaId = mediaId;
	}
	public String getMsgId() {
		return MsgId;
	}
	public void setMsgId(String msgId) {
		MsgId = msgId;
	}

	@Override
	public String toString() {
		return "In_Image [ToUserName=" + ToUserName + ", FromUserName="
				+ FromUserName + ", CreateTime=" + CreateTime + ", MsgType="
				+ MsgType + ", PicUrl=" + PicUrl + ", MediaId=" + MediaId
				+ ", MsgId=" + MsgId + "]";
	}
	
	
	
}
package net.wxinterface;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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 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���Ͳ˵�,�ͻظ�һ�������õ�Url
							String url  = map.get("EventKey");//�����View���Ͳ˵��Ļ�,���EventKey�������Url��ֵ
							if("11".equals(url)){//���Keyֵ��11
							message = 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 block
	e.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();*/
		String requestStr = In_Util.getStringFromRequest(request);
		System.out.println(requestStr);
		System.out.println("北风网");
	/*	In_Text text = new In_Text(requestStr);
		System.out.println(text);*/
	     In_Image in_Image = new In_Image(requestStr);
         System.out.println(in_Image);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

 

转载于:https://my.oschina.net/u/3206447/blog/900586

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值