//Java访问“获取token”与“个人风险标签A-身份证号”接口示例
//本代码仅供测试参考,实际生产调用时,应通过中控服务器定时刷新token,而不应在每次调用时都获取token
//定义AppId、AppSecret、Signature、URL等变量
String url = "https://openapi.unionpay.com/upapi/ARESVerificationService";
String appId = "aaaaaa";
String appSecret = "bbbbbbbb";
String sign = "cccccc";
String api = "PerRiskEval_getCertifIdFraudRiskEval";
@Test
public void testCertIdRisk() throws Exception {
JSONObject json = new JSONObject();
JSONObject data = new JSONObject();
json.put("id", "0001000001");
data.put("certId", "391EE5771E5F5D153E2594CBC760DB5CCF99940FE66B2AF9050B387BA217B50A");
data.put("queryHash", "1");
data.put("queryTp", "01");
data.put("queryFraudApply", "1");
data.put("queryEconomicCrime", "1");
data.put("queryLoseTrust", "1");
data.put("queryScalperLevel", "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");
//个人风险标签A-身份证号接口
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();
}
}