签名验签不通过检测
1. 检查字段拼接方式是否正确
2. 字符串转二进制数组时是否正确(重点)
通常我们进行二进制转换是指调用String的getByte()无参数方法,该方法在转换时默认获取当前系统的字符格式:
if (defaultCharset == null) {
synchronized (Charset.class) {
String csn = AccessController.doPrivileged(
new GetPropertyAction("file.encoding"));
Charset cs = lookup(csn);
if (cs != null)
defaultCharset = cs;
else
defaultCharset = forName("UTF-8");
}
}
如果获取字符编码异常,默认使用iso-8859-1形式:
try {
// use charset name encode() variant which provides caching.
return encode(csn, ca, off, len);
} catch (UnsupportedEncodingException x) {
warnUnsupportedCharset(csn);
}
try {
return encode("ISO-8859-1", ca, off, len);
} catch (UnsupportedEncodingException x)
.....
因此在进行签名过程中需要将字符串转换成二进制数组时,都要使用getByte(charSetName)的方式指定编码集
3. 请求方式是否正确
使用httppost请求时,无法直接通过指定header的方式设置ContentType,需在entry体中设置ContentType
HttpPost post = new HttpPost(url);
StringEntity entity = new StringEntity(json,"UTF-8");
entity.setContentType("application/json");
post.setEntity(entity);