1.jsp页面验证token时代码如下:
<%
out.print(request.getParameter("echostr"));
%>
注意:(1)只需要在页面写上述代码,jsp 头部也不要写,还要注意<%之前不要有空格或者空行
(2)公众平台配置的页面及上述代码所在页面,并且服务器所在端口必须是80端口
2.当验证通过后,修改页面代码为:
<%@ page language="java" import="java.io.*,java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>
<%-- <%
out.print(request.getParameter("echostr"));
%> --%>
<%
// 接收XML数据
BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while((line = br.readLine())!=null){
sb.append(line);
}
// 取出发送用户
String xmlS = sb.toString();
System.out.println(xmlS);
if(xmlS !=null && !xmlS.equals("")){
int fromuser_s = xmlS.indexOf("<FromUserName><![CDATA[");
int fromuser_e = xmlS.indexOf("]]></FromUserName>");
String fromuser = xmlS.substring(fromuser_s + 23, fromuser_e);
System.out.println("fromuser:"+fromuser);
// 取出目标用户
int touser_s = xmlS.indexOf("<ToUserName><![CDATA[");
int touser_e = xmlS.indexOf("]]></ToUserName>");
String touser = xmlS.substring(touser_s + 21, touser_e);
System.out.println("touser:"+touser);
// 取出发送内容
int content_s = xmlS.indexOf("<Content><![CDATA[");
int content_e = xmlS.indexOf("]]></Content>");
String content = xmlS.substring(content_s + 18, content_e);
System.out.println("content:"+content);
if(content.equals("Hello2BizUser")){//注意:Hello2BizUser是默认关注时发送的内容
out.print("<xml>");
out.print(" <ToUserName><![CDATA["+fromuser+"]]></ToUserName>");
out.print(" <FromUserName><![CDATA["+touser+"]]></FromUserName>");
out.print(" <CreateTime>"+new Date().getTime()+"</CreateTime>");
out.print(" <MsgType><![CDATA[text]]></MsgType>");
out.print(" <Content><![CDATA[ 额.....没能成功识别,元芳淡定的说,“有错别字,换个字试试!]]></Content>");
out.print(" <FuncFlag>0</FuncFlag>");
out.print("</xml>");
}
%>