完成系统注册的用户经常要进用户实名,一段简单的代码即可完成认证,这里以Java为例,其他语言处理方法类似:
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
// 公民身份证实名认证实例代码
public class IDTest {
public static void main(String[] args) {
// 创建默认的httpClient实例
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("https://service.yunyidata.com/idAuthentic");
// 创建参数队列
String merNo = "26001";
String name = "张三";
String idNo = "350783199806195231";
String md5Key = "12345678"; // 在【管理后台-->安全管理-->秘钥信息】里面获取,需先注册:yunyidata.com
String MD5Info = "";
MD5 md5 = new MD5();
String signStr = "merNo=" + merNo + "&name=" + name + "&idNo=" + idNo + "&" + md5Key;
MD5Info = md5.getMD5Info(signStr); // 例:C2A1013CBE08B4B93BCA9CDF27090846
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("merNo", merNo));
formparams.add(new BasicNameValuePair("name", name));
formparams.add(new BasicNameValuePair("idNo", idNo));
formparams.add(new BasicNameValuePair("MD5Info", MD5Info));
//======本质就是post方式传上面的4个参数,其他方法和语言都可以实现======
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
System.out.println("executing request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
String entitys = EntityUtils.toString(entity, "UTF-8");
System.out.println("--------------------------------------");
System.out.println("Response content: " + entitys);
System.out.println("--------------------------------------");
Map<String, String> data = new LinkedHashMap<String, String>();
JSONObject json = JSONObject.fromObject(entitys);
Iterator<?> it = json.keys();
// 遍历jsonObject数据,添加到Map对象
while(it.hasNext()){
String key = String.valueOf(it.next());
String values = String.valueOf(json.get(key));
if(key.equals("respMessage") || "MD5Info".equals(key)){
continue;
}
data.put(key, values);
}
String respMessage = (String) json.get("respMessage");
String matchMessage = (String) json.get("matchMessage");
System.out.println("respMessage: " + respMessage);
System.out.println("matchMessage: " + matchMessage);
//respCode respMessage
//0001 商户不存在
//0002 商户未开通
//0003 签名信息不能为空
//0004 姓名不能为空
//0005 姓名不能包含特殊字符或数字
//0006 系统异常
//0007 无此身份证号码
//0009 身份证号不能为空
//0010 身份证号格式错误,请检查后重新输入
//0013 MD5签名校验错误
//0014 IP不在白名单列表内(如果设置了ip白名单会判断此项)
//0015 余额不足,请充值
//
//0000 身份证信息匹配
//0008 身份证信息不匹配
//说明:只有结果代码为:0000、0007、0008时会扣费,其他项均不扣费。
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1万+

被折叠的 条评论
为什么被折叠?



