标签:菜单 自动回复 equals xtend tps 提交 log 开发 timestamp
终于成功了,事实证明只有托管在百度云服务器利用百度云服务器作为公网服务器才可以与腾讯服务器交互,最终tocken验证成功(提交成功)
开发模式与编辑模式是互斥的,如果开通了开发模式那么自动回复和自定义菜单就会失效
然后停用开发者模式,回过头来用ngrok来作为公网服务器试试
事实证明是不行的
完整的代码如下:
package net.wxinterface;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WX_Interface extends HttpServlet {
/**
* Constructor of the object.
*/
public WX_Interface() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//微信加密签名,signature结合了开发者填写的tocken参数和请求中的timestamp参数、nonce参数。
String signature = request.getParameter("signature");
//时间戳
String timestamp = request.getParameter("timestamp");
//随机数
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
String tocken = "test";
try{
if(null != signature){
String[] ArrTmp = {tocken,timestamp,nonce};
Arrays.sort(ArrTmp);
StringBuffer sb = new StringBuffer();
for(int i=0;i<ArrTmp.length;i++){
sb.append(ArrTmp[i]);
}
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] bytes = md.digest(new String(sb).getBytes());
StringBuffer buf = new StringBuffer();
for(int i=0;i<bytes.length;i++){
if(((int)bytes[i] & 0xff)<0x10){
buf.append("0");
}
buf.append(Long.toString((int) bytes[i] & 0xff,16));
}
if(signature.equals(buf.toString())){
response.getOutputStream().println(echostr);
}
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("test0");
/* System.out.println("doGet");
System.out.println("signature "+signature);
System.out.println("timstamp "+timestamp);
System.out.println("nonce "+nonce);
System.out.println("echostr "+echostr);*/
System.out.println("doGet");
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doPost");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
web.xml如图所示:
标签:菜单 自动回复 equals xtend tps 提交 log 开发 timestamp
原文地址:http://www.cnblogs.com/ZHONGZHENHUA/p/6270682.html