上一节讲了开发的接口接入这一部分,这是非常重要的.
实现业务的逻辑应该写在POST方法这一块.
GET是消息验证用的,验证接口那一块的.
XML数据包:腾讯服务器传过来的数据是XML格式的传到你的业务服务器上的.
开发者填写的URL:你自己的这台业务服务器了.
腾讯服务器和你的业务服务器之间有一个五秒的连接限制.也就是说你的业务服务器必须在五秒之内给腾讯服务器一个响应,这个问题必须要注意啊.以后你的程序越写越大,你还要连接业务服务器上的数据库.你要连接业务服务器上的数据库一定要保证你所有的业务自它请求过来开始,也就是说腾讯服务器的一个request过来开始到你的业务服务器response响应回去,一定要在五秒之内完成.如果五秒之内不完成当前这次请求就断掉了.断掉了之后它会说它重新再发起一次请求.这样的动作会有三次,如果三次你都是在五秒之内没有响应的话,它就会在手机端提示当前服务号无法工作或者是无法提供服务之类的信息.也就说这次请求你就失败了.你超过五秒钟之后比如说你在第10秒钟你的业务服务器查询完数据库组装完报文之后你再返回回去没有用.这个就几个解决方案,首先你就保证它五秒钟之内能执行完呗.你通过优化你的数据库,你采用其他的一种方式,比如说数据缓存啊或者是什么分布式部署啊或者是静态化啊等等,如果说这块五秒钟你的确是完成不了,你可以调用它其他的接口.其实业务服务器还可以主动向腾讯服务器发起调用,手机->腾讯服务器->业务服务器.反过来是业务服务器->腾讯服务器->手机有一个推送消息.
Source->Format.
ToUserName是开发者的微信号.
腾讯服务器有N多个账号,你的业务服务器也可以为N多个腾讯账号服务.你一个接口接收过来的消息你知道是哪个腾讯账号发送过来的吗?就是根据ToUserName来判断的.
FromUserName发送方账号:OpenID是哪个手机的号.是我的微信号:
这样第一个功能就实现了,业务服务器已经可以实现接收文本消息了.
package wx.in;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
public class In_Util {
/**
* 通过request得到请求流中的微信报文str
* @param request
* @return
*/
public static String getStringFromRequest(HttpServletRequest request){
StringBuffer strb = new StringBuffer();
//strb要存储一下request获取过来的东西
ServletInputStream in;
try {
in = request.getInputStream();
BufferedReader breader = new BufferedReader( new InputStreamReader(in,"UTF-8"));//接收是对的,已经进行转码了.
String str = null;
//+-*/是运算符,赋值运算符=也是运算符
while(null!=(str = breader.readLine())){
strb.append(str);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strb.toString();
}
}
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_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("北风网");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}