Java使⽤阿⾥云接⼝进⾏⾝份证实名认证的⽰例实现 如今随着互联⽹产业的多元化发展,尤其是互联⽹⾦融,O2O,共享经济等新兴商业形式的兴起,企业对实名认证业务的数据形式和数据质量有了更⾼的需求。如今也衍⽣出⾝份证实名认证业务,通过接⼝将⾝份证号码、姓名上传⾄阿⾥云,再与全国公民⾝份信息系统进⾏匹配,判断信息的⼀致性。 在使⽤接⼝服务的⽅⾯我推荐使⽤技术实⼒强⼤的阿⾥云;
附上:
⾸先点击:【】获取相应的订单后在控制台中可以得到您的appcode; 发送数据:
bodys.put("idNo", "340421************");
bodys.put("name", "张三");
返回数据:
{
"name": "张三",
"idNo": "340421************",
"respMessage": "⾝份证信息匹配",
"respCode": "0000",
"province": "安徽省",
"city": "淮南市",北斗接收机
"county": "凤台县",
"birthday": "19071014",
"sex": "M",
"age": "111"
}
具体实现类:
public static void main(String[] args) {
String host = "idenauthen.";
String path = "/idenAuthentication";
String method = "POST";
String appcode = "你⾃⼰的AppCode";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英⽂空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
Map<String, String> bodys = new HashMap<String, String>();
bodys.put("idNo", "340421************");
bodys.put("name", "张三");
try {
/**
* 重要提⽰如下:
* HttpUtils请从
* /aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下载
*
* 相应的依赖请参照
* /aliyun/api-gateway-demo-sign-java/blob/l
*/
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
System.out.String());
//获取response的body
//System.out.Entity()));
} catch (Exception e) {爆破玻璃
e.printStackTrace();
}
}
⼯具类HttpUtils:
ate.util.send;
import java.io.UnsupportedEncodingException;
URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
ssl.SSLContext;
ssl.TrustManager;
ssl.X509TrustManager;
import s.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.ity.UrlEncodedFormEntity;
import org.apache.hods.HttpDelete;
import org.apache.hods.HttpGet;
import org.apache.hods.HttpPost;
import org.apache.hods.HttpPut;
import org.ClientConnectionManager;
import org.scheme.Scheme;
import org.scheme.SchemeRegistry;
import org.ssl.SSLSocketFactory;
import org.ity.ByteArrayEntity;
import org.ity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.ssage.BasicNameValuePair;
public class HttpUtils {
/**
* get
*
* @param host
* @param path
* @param method
发电机冷却器
* @param headers
* @param querys
* @return
* @throws Exception
*/
public static HttpResponse doGet(String host, String path, String method, Map<String, String> headers,
Map<String, String> querys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpGet request = new HttpGet(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : Set()) {
request.Key(), e.getValue());
}
ute(request);
}
/**
* post form
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param bodys
* @return
* @throws Exception
*/
public static HttpResponse doPost(String host, String path, String method, Map<String, String> headers,
Map<String, String> querys,
Map<String, String> bodys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : Set()) {
request.Key(), e.getValue());
}
if (bodys != null) {
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
for (String key : bodys.keySet()) {
nameValuePairList.add(new BasicNameValuePair(key, (key)));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8"); formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
request.setEntity(formEntity);
}
ute(request);
}
/**
* Post String
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
红外线测高仪public static HttpResponse doPost(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
String body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : Set()) {
request.Key(), e.getValue());
}
if (StringUtils.isNotBlank(body)) {
request.setEntity(new StringEntity(body, "utf-8"));
}
ute(request);
}
/
**
* Post stream
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPost(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
byte[] body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : Set()) {
request.Key(), e.getValue());
}
if (body != null) {
request.setEntity(new ByteArrayEntity(body));
}
ute(request);
}
/**
* Put String
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPut(String host, String path, String method, Map<String, String> headers,
Map<String, String> querys,
String body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : Set()) {
request.Key(), e.getValue());
}
if (StringUtils.isNotBlank(body)) {
手机背光request.setEntity(new StringEntity(body, "utf-8"));
}
ute(request);
}
/**
* Put stream
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPut(String host, String path, String method, Map<String, String> headers,保健油
Map<String, String> querys,
byte[] body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : Set()) {
request.Key(), e.getValue());
}
if (body != null) {
request.setEntity(new ByteArrayEntity(body));
}
ute(request);
}
/
**
* Delete
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @return
* @throws Exception
*/
public static HttpResponse doDelete(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : Set()) {
request.Key(), e.getValue());
}
ute(request);
}
private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException { StringBuilder sbUrl = new StringBuilder();
sbUrl.append(host);
if (!StringUtils.isBlank(path)) {
sbUrl.append(path);
}
if (null != querys) {
StringBuilder sbQuery = new StringBuilder();
for (Map.Entry<String, String> query : Set()) {
if (0 < sbQuery.length()) {
sbQuery.append("&");
}
if (StringUtils.Key()) && !StringUtils.Value())) {
sbQuery.Value());
}
if (!StringUtils.Key())) {
sbQuery.Key());
if (!StringUtils.Value())) {
sbQuery.append("=");
sbQuery.Value(), "utf-8"));
}
}
}
if (0 < sbQuery.length()) {
sbUrl.append("?").append(sbQuery);
}
}
String();
}
private static HttpClient wrapClient(String host) {
HttpClient httpClient = new DefaultHttpClient();
if (host.startsWith("")) {
sslClient(httpClient);
}
return httpClient;
}
private static void sslClient(HttpClient httpClient) {
try {
SSLContext ctx = Instance("TLS");
X509TrustManager tm = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] xcs, String str) {
}
public void checkServerTrusted(X509Certificate[] xcs, String str) {
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = ConnectionManager();
SchemeRegistry registry = SchemeRegistry();