1 验证URL时,需要原样返回echostr。验证则将传递的timestamp,TOKEN,nonce3个参数,放到list中,然后排序,取出组合成string,计算sha1是否与传递的signature一致
2 对消息的处理,api讲微信服务器post消息到开发者的服务器,这时候就需要对收到的数据进行处理。
/**
* 解析微信发来的请求(XML)
*
* @param request
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
// 将解析结果存储在HashMap中
Map<String, String> map = new HashMap<String, String>();
// 从request中取得输入流
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList)
map.put(e.getName(), e.getText());
// 释放资源
inputStream.close();
inputStream = null;
return map;
}来源:http://blog.csdn.net/lyq8479/article/details/8949088
原文:http://xiaosa.blog.51cto.com/665033/1703200