调用示例
更新时间:2023-11-13
请看完API文档参考后再看本篇示例
AES版本调用示例(以银行卡四要素为例):
@Test
public void testApi() {
String localUrl = "见已购列表";
InvokeRequest invokeRequest = new InvokeRequest();
// appKey
Long timestamp = System.currentTimeMillis() / 1000;
Map<String, Object> paramMap = new HashMap<>();
String mobile = "手机号";
String name = "姓名";
String idNum = "身份证号";
String cardNo = "银行卡号";
paramMap.put("mobile", mobile);
paramMap.put("name", name);
paramMap.put("idNum", idNum);
paramMap.put("cardNo", cardNo);
String encrypt = AesEncryptUtil.encrypt(JSON.toJSONString(paramMap), "sk");
invokeRequest.setParamEncrypt(encrypt);
invokeRequest.setReqEncryptType(1);
String secretKey = "sk";
StringBuilder assemble = AssembleUtil.assembleParameters(new HashMap<>());
if (StringUtils.isNotEmpty(invokeRequest.getParamEncrypt())) {
assemble.append(invokeRequest.getParamEncrypt()).append("&");
}
assemble.append(timestamp).append("&").append(secretKey);
System.out.println("assemble:" + assemble.toString());
String signature = MD5Util.md5Encode(assemble.toString());
String bodyJson = JSON.toJSONString(invokeRequest);
List<Header> headers = new ArrayList<>();
Header appKeyHeader = new BasicHeader("appkey", "ak");
Header timeStampHeader = new BasicHeader("timestamp",
String.valueOf(timestamp));
Header msgEncryptionHeader = new BasicHeader("signature", signature);
headers.add(appKeyHeader);
headers.add(timeStampHeader);
headers.add(msgEncryptionHeader);
System.out.println(bodyJson);
System.out.println(HttpsUtil.post(localUrl, headers.toArray(new Header[]{}), bodyJson,
2000, 30000));
}
// assemble方法
public class AssembleUtil {
public static <T> StringBuilder assembleParameters(Map<String, T> paramMap) {
StringBuilder result = new StringBuilder();
if (paramMap == null || paramMap.isEmpty()) {
return result;
}
paramMap.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).forEach(entry ->
result.append(entry.getKey()).append("=").append(entry.getValue()).append("&"));
return result;
}
}
// md5方法
public static String md5Encode(String plainText) {
byte[] secretBytes;
try {
secretBytes = MessageDigest.getInstance("md5").digest(plainText
.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("无此md5算法");
}
String md5code = (new BigInteger(1, secretBytes)).toString(16);
return StringUtils.leftPad(md5code, 32, "0");
}
// AES加密方法
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("mobile", mobile);
String s = JSON.toJSONString(paramMap);
String encrypt = AesEncryptUtil.encrypt(s, {secretKey});
public static String encrypt(String content, String encryptKey) {
String key = encryptKey;
if (encryptKey.length() > 64) {
key = encryptKey.substring(0, DEFAULT_KEY_LEN);
}
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
byte[] b = cipher.doFinal(content.getBytes("utf-8"));
return Base64.encodeBase64String(b);
} catch (Exception e) {
log.info("AesEncryptUtil encrypt catch e: {}", e);
return null;
}
}
明文版本调用示例(以银行卡三要素为例)
@Test
public void testTelecomApi() {
String localUrl = "见已购列表";
InvokeRequest invokeRequest = new InvokeRequest();
Long timestamp = System.currentTimeMillis() / 1000;
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("mobile", "手机号");
paramMap.put("name", "姓名");
paramMap.put("idNum", "身份证号");
invokeRequest.setParameters(paramMap);
String secretKey = "提供的sk";
StringBuilder assemble = AssembleUtil.assembleParameters(paramMap);
assemble.append(timestamp).append("&").append(secretKey);
String signature = md5Encode(assemble.toString());
String bodyJson = JSON.toJSONString(invokeRequest);
List<Header> headers = new ArrayList<>();
Header appKeyHeader = new BasicHeader("appkey", "提供的ak");
Header timeStampHeader = new BasicHeader("timestamp",
String.valueOf(timestamp));
Header msgEncryptionHeader = new BasicHeader("signature", signature);
headers.add(appKeyHeader);
headers.add(timeStampHeader);
headers.add(msgEncryptionHeader);
System.out.println(HttpsUtil.post(localUrl, headers.toArray(new Header[]{}), bodyJson,
2000, 30000));
}
// assemble方法
public class AssembleUtil {
public static <T> StringBuilder assembleParameters(Map<String, T> paramMap) {
StringBuilder result = new StringBuilder();
if (paramMap == null || paramMap.isEmpty()) {
return result;
}
paramMap.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).forEach(entry ->
result.append(entry.getKey()).append("=").append(entry.getValue()).append("&"));
return result;
}
}
// md5方法
public static String md5Encode(String plainText) {
byte[] secretBytes;
try {
secretBytes = MessageDigest.getInstance("md5").digest(plainText
.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("无此md5算法");
}
String md5code = (new BigInteger(1, secretBytes)).toString(16);
return StringUtils.leftPad(md5code, 32, "0");
}