使用微信公众测试号无法校验通过
在做微信公众号的开发,因微信公众号还未申请下来,先申请注册了微信公众号测试账号先进行开发,但微信公众号测试账号总是无法校验通过,程序总是报如下错误:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
java.util.Arrays.sort(Arrays.java:472)
com.sttx.weixin.util.SignUtil.checkSignature(SignUtil.java:32)
com.sttx.weixin.servlet.CoreServlet.doGet(CoreServlet.java:40)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.41 logs.
我的程序部署在百度BAE上,域名:http://xxx.duapp.com;运行一个测试显示页正常;
申请的微信公众测试号中的URL:http://xxx.duapp.com/coreServlet(填写了百度BAE的域名),Token:weixin(与程序中的一致)
程序如下:
package com.sttx.weixin.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
/**
* @Title: SignUtil
* @Description:请求校验工具类
* @Version:
* @author:
* @date:
*/
public class SignUtil {
// 与开发模式接口配置信息中Token保持一致
private static String token = "weixin";
/**
*
* @Description:校验签名
* @param:signature 微信加密签名;timestamp 时间戳;nonce 随机数;
* @return:
* @exception:
*/
public static boolean checkSignature(String signature, String timestamp,
String nonce) {
// 对token,timestamp,nonce按字典排序
String[] parmaArr = new String[] { token, timestamp, nonce };
Arrays.sort(parmaArr);
// 将排序后的结果拼接成一个字符串
StringBuilder content = new StringBuilder();
for (int i = 0; i < parmaArr.length; i++) {
content.append(parmaArr[i]);
}
// 对拼接后的字符串进行SHA-1加密
String ciphertext = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
// 拼接后字符串转换成字节数组
byte[] digest = md.digest(content.toString().getBytes());
// 将字节数组转换成字符串
ciphertext = byteArrayToHexString(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 将SHA-1加密后的字符串与signature进行对比;
return ciphertext != null ? ciphertext.equals(signature.toUpperCase()): false;
}
/**
*
* @Description:将加密byte数组转换成十六进制字符串
* @param:byteArray字符数组
* @return:
* @exception:
*/
public static String byteArrayToHexString(byte[] byteArray) {
String strDigest = "";
for (int i = 0; i < byteArray.length; i++) {
strDigest = strDigest + byteToHexString(byteArray[i]);
}
return strDigest;
}
/**
*
* @Description:将byte转换为十六进制字符
* @param:bt
* @return:hexStr 十六进制字符
* @exception:
*/
public static String byteToHexString(byte bt) {
char digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
char temp[] = new char[2];
temp[0] = digits[(bt >>> 4) & 0X0F];
temp[1] = digits[bt & 0X0F];
String hexStr = new String(temp);
return hexStr;
}
}
package com.sttx.weixin.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sttx.weixin.util.SignUtil;
/**
*
* @Title: CoreServlet
* @Description:请求处理的核心类
* @Version:
* @author:
* @date:
*/
@SuppressWarnings("serial")
@WebServlet(urlPatterns = "/coreServlet")
public class CoreServlet extends HttpServlet {
/**
* 请求校验(确认请求来自微信服务器)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//微信加密签名
String signature = request.getParameter("signature");
//时间戳
String timestamp = request.getParameter("timestamp");
//随机数
String nonce = request.getParameter("nonce");
//随机字符串
String echostr = request.getParameter("echostr");
PrintWriter out = response.getWriter();
//请求校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
if(SignUtil.checkSignature(signature, timestamp, nonce)){
out.print(echostr);
}
out.close();
out = null;
}
/**
* 请求校验与处理微信服务器发来的消息
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
在浏览器中输入http://xxx.duapp.com/coreServlet后,报错如上。请高手指教一下,在哪里出现了问题???如何改正