Java tutorial
package com.hwsoft.util.idcard; import com.nciic.serv.webservice.ServiceInf; import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codehaus.xfire.client.Client; import org.codehaus.xfire.client.XFireProxy; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import org.codehaus.xfire.transport.http.CommonsHttpMessageSender; import org.codehaus.xfire.transport.http.EasySSLProtocolSocketFactory; import org.codehaus.xfire.util.dom.DOMOutHandler; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.tree.DefaultAttribute; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Proxy; import java.net.MalformedURLException; import java.util.Date; import java.util.Iterator; import java.util.List; /** * NCIIS ??? * * @author wangzhen * @since 2013-08-12 */ public class IDCardVerifyTools { /** * NCIIS?? */ public static final String SERVICE_URL = "https://api.nciic.com.cn/nciic_ws/services/"; /** * ??? */ public static final String SERVICE_NAME = "NciicServices"; /** * ? */ public static final String LICENSE = "license.txt"; private static Log logger = LogFactory.getLog(IDCardVerifyTools.class); /** * Nciic * * @return * @throws MalformedURLException */ public static String executeNciicClient(String condition) { String result = null; ServiceInf service = null; try { ProtocolSocketFactory easy = new EasySSLProtocolSocketFactory(); Protocol protocol = new Protocol("https", easy, 443); Protocol.registerProtocol("https", protocol); Service serviceModel = new ObjectServiceFactory().create(ServiceInf.class, "service", null, null); service = (ServiceInf) new XFireProxyFactory().create(serviceModel, SERVICE_URL + SERVICE_NAME); Client client = ((XFireProxy) Proxy.getInvocationHandler(service)).getClient(); client.addOutHandler(new DOMOutHandler()); // client.setProperty(CommonsHttpMessageSender.GZIP_ENABLED, Boolean.TRUE); // client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "1"); client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0"); } catch (MalformedURLException e) { System.out.println("idcard bind erroinfo:?"); throw new RuntimeException("?", null); } //??????? String licensecode = null; BufferedReader in; try { InputStream is = IDCardVerifyTools.class.getClassLoader() .getResourceAsStream("com/hwsoft/util/idcard/" + LICENSE); in = new BufferedReader(new InputStreamReader(is)); licensecode = in.readLine(); //?? result = service.nciicCheck(licensecode, condition); } catch (Exception e) { logger.info("idcard bind erroinfo:???"); throw new RuntimeException("???", null); } return result; } /** * ?Nciicl? * * @param backXMl? * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static IdCardInfo parseXml(String backXMl, String userName, String idCard) { Document doc = null; String result_gmsfhm = null; String result_xm = null; String gmsfhm = null; String xm = null; String xp = null; String errormesage = null; IdCardInfo IdCardInfo = null; System.out.println("idcard bind Nciic backInfo:" + backXMl); try { //XML doc = DocumentHelper.parseText(backXMl); // ? Element rootElt = doc.getRootElement(); if ("RESPONSE".equalsIgnoreCase(rootElt.getName())) { //??? List<DefaultAttribute> list = rootElt.attributes(); for (DefaultAttribute e : list) { if ("errorcode".equals(e.getName())) { if ("-70".equalsIgnoreCase(e.getValue())) { throw new RuntimeException("?", null); } else if ("-71".equalsIgnoreCase(e.getValue())) { throw new RuntimeException("?", null); } else if ("-72".equalsIgnoreCase(e.getValue())) { throw new RuntimeException("IP ???", null); } else if ("-80".equalsIgnoreCase(e.getValue())) { throw new RuntimeException("??", null); } } } } else if ("ROWS".equalsIgnoreCase(rootElt.getName())) { Iterator iterForRow = rootElt.elementIterator("ROW"); while (iterForRow.hasNext()) { Element recordEleForRow = (Element) iterForRow.next(); //????? Iterator itersForInput = recordEleForRow.elementIterator("INPUT"); while (itersForInput.hasNext()) { Element recordEleForInput = (Element) itersForInput.next(); gmsfhm = recordEleForInput.elementTextTrim("gmsfhm"); if (!(idCard.equalsIgnoreCase(gmsfhm))) { throw new RuntimeException("?????", null); } xm = recordEleForInput.elementTextTrim("xm"); if (!(userName.equalsIgnoreCase(xm))) { throw new RuntimeException("?????", null); } } //?????? Iterator itersForOutput = recordEleForRow.elementIterator("OUTPUT"); while (itersForOutput.hasNext()) { Element recordEleForSubRow = (Element) itersForOutput.next(); Iterator itersForItem = recordEleForSubRow.elementIterator("ITEM"); while (itersForItem.hasNext()) { Element recordEleForItem = (Element) itersForItem.next(); gmsfhm = recordEleForItem.elementTextTrim("gmsfhm"); xm = recordEleForItem.elementTextTrim("xm"); xp = recordEleForItem.elementTextTrim("xp"); errormesage = recordEleForItem.elementTextTrim("errormesage"); if (gmsfhm != null) { result_gmsfhm = recordEleForItem.elementTextTrim("result_gmsfhm"); } if (xm != null) { result_xm = recordEleForItem.elementTextTrim("result_xm"); } if (xp != null) { xp = recordEleForItem.elementTextTrim("xp"); } if (errormesage != null) { errormesage = recordEleForItem.elementTextTrim("errormesage"); break; } } } if (null != errormesage) { throw new RuntimeException("??!", null); } else { if (!"".equalsIgnoreCase(result_xm)) { throw new RuntimeException("?????", null); } if (!"".equalsIgnoreCase(result_gmsfhm)) { throw new RuntimeException("?????", null); } IdCardInfo = new IdCardInfo(); IdCardInfo.setIdCardNumber(idCard); IdCardInfo.setRealName(userName); IdCardInfo.setAvatar(xp); } } } } catch (DocumentException e) { System.out.println("idcard bind erroinfo:XML??"); } catch (Exception e) { System.out.println("idcard bind erroinfo:XML??"); } return IdCardInfo; } /** * ????? * * @return * @throws MalformedURLException */ public static IdCardInfo verifyIDCard(String userName, String idcard) { String condition = "<?xml version=\'1.0\' encoding=\'UTF-8\' ?><ROWS><INFO><SBM>shshshsh53080</SBM>" + "</INFO><ROW><GMSFHM>??</GMSFHM><XM>??</XM></ROW>" + "<ROW FSD=\'4864\' YWLX=\'\' ><GMSFHM>" + idcard + "</GMSFHM>" + "<XM>" + userName + "</XM></ROW></ROWS>"; IdCardInfo IdCardInfo = null; try { // String result = executeNciicClient(condition); IdCardInfo = parseXml(result, userName, idcard); if (null != IdCardInfo) { IDVerifierUtil idVerifierUtil = new IDVerifierUtil(idcard); String province = ""; String city = ""; String region = ""; if (null != idVerifierUtil.getProvince()) { province = idVerifierUtil.getProvince(); } if (null != idVerifierUtil.getCity()) { city = idVerifierUtil.getCity(); } if (null != idVerifierUtil.getRegion()) { region = idVerifierUtil.getRegion(); } IdCardInfo.setAddress(province + city + region); IdCardInfo.setDate(idVerifierUtil.getBirthday()); if ("".equalsIgnoreCase(idVerifierUtil.getGender())) { IdCardInfo.setGener("MALE"); } if ("".equalsIgnoreCase(idVerifierUtil.getGender())) { IdCardInfo.setGener("FEMALE"); } IdCardInfo.setValiDate(new Date()); } } catch (Exception e) { System.out.println("idcard bind erroinfo:" + e.getMessage()); } return IdCardInfo; } /** * * * @param args * @throws MalformedURLException */ public static void main(String[] args) throws MalformedURLException { // System.out.println(IDCardVerifyTools.class.getClassLoader().getResourceAsStream("com/hwsoft/util/idcard/"+LICENSE)); verifyIDCard("?", "370921198704043318"); } }