利用javaweb的Servlet来进行“接口配置信息配置信息”认证的。
在学习微信公众号开发的时候,读到填写服务器配置的帮助部分,总是不能理解为啥按照他的步骤做总是设置失败
设置失败的一个重要原因:url的请求相应没有返回echostr这个参数。
URL:
代码:
packagecom.bs.util;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjava.io.IOException;
importjava.io.PrintWriter;
@WebServlet("/portal")
public classServerPortalextendsHttpServlet {
private static final longserialVersionUID=1L;
private static finalStringtoken="djp";
/**
*@seeHttpServlet#HttpServlet()
*/
publicServerPortal() {
super();
}
/**
*@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected voiddoGet(HttpServletRequest request,HttpServletResponse response)throwsServletException,IOException {
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
System.out.println("signature:"+ signature);
System.out.println("timestamp:"+ timestamp);
System.out.println("nonce:"+ nonce);
System.out.println("echostr:"+ echostr);
PrintWriter pw =null;
try{
pw = response.getWriter();
}catch(IOException e) {
e.printStackTrace();
}
pw.append(echostr);
pw.flush();
}
/**
*@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected voiddoPost(HttpServletRequest request,HttpServletResponse response)throwsServletException,IOException {
doGet(request,response);
}
}