package com.sunyang;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.digest.DigestUtils;
import org.dom4j.DocumentException;
/**
* 接入
* @author 孙洋
* */
public class WxServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//设置字符集 utf-8
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//获取输出
PrintWriter out = resp.getWriter();//响应
//开始接入
connect(req,out);
}
//接受数据
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//设置字符集 utf-8
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//回送信息
echoMsg(req,resp);
}
/**
* 回送消息
* 自动回复消息
*、获取XML数据
*
*2、XML -> 解析 -> 存入Map中 -> XML数据
*3、XML数据转化成对象
*4、将对象中的属性 fromUserName ---- ToUserName 调换位置,更改时间,内容
*5、将对象在转化成XML
*6、resp发送出去
*
* @param req
* @param resp
* @throws IOException
* */
public void echoMsg(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// TODO Auto-generated method stub
PrintWriter writer = resp.getWriter();
Map<String , String > xmlMap = null ;
try {
//XML -> 解析 -> 存入Map中 -> XML数据
xmlMap = MessageUntil.toMap(req.getInputStream());
Message message = MessageUntil.toMessage(xmlMap);
String from = message.getFromUserName();
String to = message.getToUserName() ;
message.setFromUserName(to);
message.setToUserName(from);
message.setCreateTime(System.currentTimeMillis());
//判断内容是否为空
String content = message.getContent() ;
content = null == content?"":content.trim();
//书写返回数据
String reply = "" ;
//获取消息类型
String type = xmlMap.get("MsgType");
//比较
if(type.equals(MessageType.TEXT)){
if(content.equals("1")||content.equalsIgnoreCase("AutoHome")){
reply = sendTextAuto();
}else if(content.equals("2")||content.equalsIgnoreCase("Tempture")){
reply = sendTextTemp();
}else{
//reply = sentTextError() ;
//reply = sentTexmainMenu();
reply = sendWeather(content);
}
}else if(type.equals(MessageType.EVENT)){
String eventType = xmlMap.get("Event").toLowerCase();//忽略大小写
if(eventType.equals(MessageType.SUBSCRIBE)){
reply = sentTexmainMenu();
message.setMsgType(MessageType.TEXT);
}else if(eventType.equals(MessageType.UNSUBSCRIBE)){
System.out.println("推出了");
message.setMsgType(MessageType.TEXT);
}
}
//设置回复的信息
message.setContent(reply);
//将message对新疆转化为xml格式
String xmlStr = MessageUntil.toXml(message);
System.out.println(xmlStr);
//使用writer对象发送
writer.print(xmlStr);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//天气
private String sendWeather(String content) {
// TODO Auto-generated method stub
content = content.replace("市","");
//获取天气字符串
String weatherStr = WeatherUntil.getWeather(content);
//处理null
if(null == weatherStr || (weatherStr = weatherStr.trim()).equals("")){
return other(content);
}
return weatherStr;
}
private String other(String content) {
// TODO Auto-generated method stub
return "您发送的信息为: " + content;
}
private String sentTexmainMenu() {
// TODO Auto-generated method stub
StringBuffer buffer = new StringBuffer();
buffer.append("家庭安全智能报警系统 -- 菜单") ;
return buffer.toString();
}
private String sentTextError() {
// TODO Auto-generated method stub
StringBuffer buffer = new StringBuffer();
buffer.append("家庭安全智能报警系统通知您,您的消息有误") ;
return buffer.toString();
}
private String sendTextTemp() {
// TODO Auto-generated method stub
StringBuffer buffer = new StringBuffer();
buffer.append("家庭安全智能报警系统 -- 温度") ;
return buffer.toString();
}
private String sendTextAuto() {
// TODO Auto-generated method stub
StringBuffer buffer = new StringBuffer();
buffer.append("家庭安全智能报警系统") ;
return buffer.toString();
}
/**
* 接入
* @param req
* @param out
* */
private void connect(HttpServletRequest req, PrintWriter out) {
// TODO Auto-generated method stub
//获取参数
String signature = req.getParameter("signature");//微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
String timestamp = req.getParameter("timestamp");//时间戳
String nonce = req.getParameter("nonce");//随机数
String echostr = req.getParameter("echostr");//随机字符串
//校验
List<String> list = new ArrayList<String>();
list.add("AutoHome");
list.add(timestamp);
list.add(nonce);
//开始排序
Collections.sort(list);
//拼接字符串 --》进行加密
StringBuffer buffer = new StringBuffer();
for(String string : list ){
buffer.append(string);
}
//使用apache commons codec 进行加密
String shalStr = DigestUtils.shaHex(buffer.toString());
//做对比
boolean flag = shalStr.equals(signature) ;
if(flag){
System.out.println("接入成功");
out.print(echostr);
out.flush();
}
}
}
这是我的主要代码。但是,通过手机微信后台输入的时候,说接受不到信息,接收到的为Null,