1、配置接口信息
在上一个文章中提到了去微信公众平台申请测试账号和用映射软件生成外网地址,在这里我们要到测试账号管理那里去配置接口信息。
需要配置一个验证服务器有效性的地址和token(这个可以任意填写)还有消息的加密方式。
2、验证地址有效性
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String signature = req.getParameter("signature");
String timestamp = req.getParameter("timestamp");
String nonce = req.getParameter("nonce");
String echostr = req.getParameter("echostr");
PrintWriter out = resp.getWriter();
if(CheckUtil.checkSignature(signature, timestamp, nonce)){
out.print(echostr);
}
}
public class CheckUtil {
private static final String token = "myfirstweixin";
public static boolean checkSignature(String signature,String timestamp,String nonce){
String[] arr = new String[]{token,timestamp,nonce};
//排序
Arrays.sort(arr);
//生成字符串
StringBuffer content = new StringBuffer();
for(int i=0;i
>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
}
return new String(buf);
} catch (Exception e) {
return null;
}
}
}
当服务器地址校验完后就开始起作用,可以开始你的微信公众号开发之旅了。