//本代码仅供测试参考,实际生产调用时,应通过中控服务器定时刷新token,而不应在每次调用时都获取token
//定义AppId、AppSecret、Signature、URL等变量
String url = "https://openapi.unionpay.com/upapi/capitalFlows";
String appId = "aaaaaa";
String appSecret = "bbbbbbbb";
String sign = "cccccc";
String api = "debitCard";
@Test
public void testCertIdRisk() throws Exception {
JSONObject json = new JSONObject();
JSONObject data = new JSONObject();
json.put("id", "0001000001");
data.put("cardHash", "A7426D8ED6AAA776781C6A0A8DAABF8882AFFC3D9206E7DEC72B96E26937011A");
data.put("certHash", "391EE5771E5F5D153E2594CBC760DB5CCF99940FE66B2AF9050B387BA217B50A");
data.put("queryTp", "03");
data.put("cateId", "01");
data.put("authSeq", "00010000012022081209455088888888");
data.put("protocolVersion", "xx_bank_app_V1.0");
data.put("startDt", "20220101");
data.put("endDt", "20220110");
data.put("queryBuild", "1");
data.put("queryStock", "1");
data.put("queryLoan", "1");
data.put("queryGamb", "1");
data.put("queryBusiness", "1");
data.put("queryTax", "1");
json.put("data", data);
//获取token接口
String result = httpGet(url + "/token?app_id=" + appId + "&app_secret=" + appSecret);
net.sf.json.JSONObject json_result = net.sf.json.JSONObject.fromObject(result);
String token = json_result.getString("token");
//借记卡资金流向接口
long ts = System.currentTimeMillis();
sign = EncodeUtils.encodeBySHA256(sign + json.toString() + ts);
String res = httpPostWithJSON(url + "/" + api + "?token=" + token + "&sign=" + sign + "&ts=" + ts, json.toString());
System.out.println("响应参数为:"+ res);
}
public String httpPostWithJSON(String url, String encoderJson)
throws Exception {
// 将JSON进行UTF-8编码,以便传输中文
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
StringEntity se = new StringEntity(encoderJson, "UTF8");
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
net.sf.json.JSONObject json = toJson(content);
return json.toString();
}
public String httpGet(String url)
throws Exception {
// 将JSON进行UTF-8编码,以便传输中文
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
net.sf.json.JSONObject json = toJson(content);
return json.toString();
}
public static net.sf.json.JSONObject toJson(InputStream in)
throws UnsupportedEncodingException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF8"));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
String string = sb.toString();
if (string != null && !string.equals("")) {
return net.sf.json.JSONObject.fromObject(string);
} else {
return new net.sf.json.JSONObject();
}
}