-
Notifications
You must be signed in to change notification settings - Fork 1.1k
消息事件接收
guoleijie edited this page Jul 4, 2018
·
8 revisions
package weixin.popular.example;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import weixin.popular.bean.message.EventMessage;
import weixin.popular.bean.xmlmessage.XMLMessage;
import weixin.popular.bean.xmlmessage.XMLTextMessage;
import weixin.popular.support.ExpireKey;
import weixin.popular.support.expirekey.DefaultExpireKey;
import weixin.popular.util.SignatureUtil;
import weixin.popular.util.XMLConverUtil;
/**
* 服务端事件消息接收
*
* @author Yi
*/
public class ReceiveServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
//重复通知过滤
private static ExpireKey expireKey = new DefaultExpireKey();
//从官方获取
private String token = "test";
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletOutputStream outputStream = response.getOutputStream();
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
//首次请求申请验证,返回echostr
if (echostr != null) {
outputStreamWrite(outputStream, echostr);
return;
}
//验证请求签名
if (!signature.equals(SignatureUtil.generateEventMessageSignature(token, timestamp, nonce))) {
System.out.println("The request signature is invalid");
return;
} else {
outputStreamWrite(outputStream, echostr);
return;
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletInputStream inputStream = request.getInputStream();
ServletOutputStream outputStream = response.getOutputStream();
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
//首次请求申请验证,返回echostr
if (echostr != null) {
outputStreamWrite(outputStream, echostr);
return;
}
//验证请求签名
if (!signature.equals(SignatureUtil.generateEventMessageSignature(token, timestamp, nonce))) {
System.out.println("The request signature is invalid");
return;
}
if (inputStream != null) {
//转换XML
EventMessage eventMessage = XMLConverUtil.convertToObject(EventMessage.class, inputStream);
String key = eventMessage.getFromUserName() + "__"
+ eventMessage.getToUserName() + "__"
+ eventMessage.getMsgId() + "__"
+ eventMessage.getCreateTime();
if (expireKey.exists(key)) {
//重复通知不作处理
return;
} else {
expireKey.add(key);
}
//创建回复
XMLMessage xmlTextMessage = new XMLTextMessage(
eventMessage.getFromUserName(),
eventMessage.getToUserName(),
"你好");
//回复
xmlTextMessage.outputStreamWrite(outputStream);
return;
}
outputStreamWrite(outputStream, "");
}
/**
* 数据流输出
*/
private boolean outputStreamWrite(OutputStream outputStream, String text) {
try {
outputStream.write(text.getBytes("utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
}
获取token
- TokenAPI access_token 获取
- MediaAPI 多媒体上传下载(临时素材)
- MaterialAPI 永久素材
- MenuAPI 菜单、个性化菜单
- MessageAPI 信息发送(客服消息、群发消息、模板消息)
- PayAPI 支付订单相关接口
- PayMchAPI 支付订单、红包、企业付款、代扣费(商户平台版)
- QrcodeAPI 二维码
- SnsAPI 网签授权
- UserAPI 用户管理、分组、标签、黑名单
- ShorturlAPI 长链接转短链接
- TicketAPI JSAPI ticket
- ComponentAPI 第三方平台开发
- CallbackipAPI 获取微信服务器IP地址
- ClearQuotaAPI 接口调用频次清零
- PoiAPI 微信门店 @Moyq5 (贡献)
- CardAPI 微信卡券 @Moyq5 (贡献)
- ShakearoundAPI 微信摇一摇周边 @Moyq5 (贡献)
- DatacubeAPI 数据统计 @Moyq5 (贡献)
- CustomserviceAPI 客服功能 @ConciseA (贡献)