今天项目里用到了阿里云的实名认证接口,具体如下:
1、身份证实名认证接口,用于验证身份证信息,具体代码如下:
1 String host = "http://telvertify.market.alicloudapi.com"; 2 String path = "/lianzhuo/telvertify"; 3 String method = "GET"; 4 String appcode = "3367468e1a014b8db4be6d53304be6d2"; 5 Map<String, String> headers = new HashMap<String, String>(); 6 //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 7 headers.put("Authorization", "APPCODE " + appcode); 8 Map<String, String> querys = new HashMap<String, String>(); 9 querys.put("id", "511529199306113473"); 10 querys.put("name", "空海贤"); 11 querys.put("telnumber", "18057167427"); 12 13 try { 14 /** 15 * 重要提示如下: 16 * HttpUtils请从 17 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java 18 * 下载 19 * 20 * 相应的依赖请参照 21 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml 22 */ 23 HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys); 24 System.out.println(response.toString()); 25 //获取response的body 26 String s=EntityUtils.toString(response.getEntity()); 27 s=new String(s.getBytes("ISO-8859-1")); 28 System.out.println(s); 29 } catch (Exception e) { 30 e.printStackTrace(); 31 }
2、公司实名认证接口,用于验证公司信息,具体如下:
1 String host = "http://cverify.market.alicloudapi.com"; 2 String path = "/lianzhuo/cvertify"; 3 String method = "GET"; 4 String appcode = "3367468e1a014b8db4be6d53304be6d2"; 5 Map<String, String> headers = new HashMap<String, String>(); 6 //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 7 headers.put("Authorization", "APPCODE " + appcode); 8 Map<String, String> querys = new HashMap<String, String>(); 9 querys.put("code", "91110000802100433B"); 10 querys.put("company", "北京百度网讯科技有限公司"); 11 querys.put("legal", "梁志祥"); 12 13 try { 14 /** 15 * 重要提示如下: 16 * HttpUtils请从 17 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java 18 * 下载 19 * 20 * 相应的依赖请参照 21 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml 22 */ 23 HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys); 24 System.out.println(response.toString()); 25 //获取response的body 26 System.out.println(EntityUtils.toString(response.getEntity())); 27 } catch (Exception e) { 28 e.printStackTrace(); 29 }
补充:HttpUtils代码如下:
1 package Util; 2 import java.io.UnsupportedEncodingException; 3 import java.net.URLEncoder; 4 import java.security.KeyManagementException; 5 import java.security.NoSuchAlgorithmException; 6 import java.security.cert.X509Certificate; 7 import java.util.ArrayList; 8 import java.util.List; 9 import java.util.Map; 10 11 import javax.net.ssl.SSLContext; 12 import javax.net.ssl.TrustManager; 13 import javax.net.ssl.X509TrustManager; 14 15 import org.apache.commons.lang.StringUtils; 16 import org.apache.http.HttpResponse; 17 import org.apache.http.NameValuePair; 18 import org.apache.http.client.HttpClient; 19 import org.apache.http.client.entity.UrlEncodedFormEntity; 20 import org.apache.http.client.methods.HttpDelete; 21 import org.apache.http.client.methods.HttpGet; 22 import org.apache.http.client.methods.HttpPost; 23 import org.apache.http.client.methods.HttpPut; 24 import org.apache.http.conn.ClientConnectionManager; 25 import org.apache.http.conn.scheme.Scheme; 26 import org.apache.http.conn.scheme.SchemeRegistry; 27 import org.apache.http.conn.ssl.SSLSocketFactory; 28 import org.apache.http.entity.ByteArrayEntity; 29 import org.apache.http.entity.StringEntity; 30 import org.apache.http.impl.client.DefaultHttpClient; 31 import org.apache.http.message.BasicNameValuePair; 32 33 public class HttpUtils { 34 35 /** 36 * get 37 * 38 * @param host 39 * @param path 40 * @param method 41 * @param headers 42 * @param querys 43 * @return 44 * @throws Exception 45 */ 46 public static HttpResponse doGet(String host, String path, String method, 47 Map<String, String> headers, 48 Map<String, String> querys) 49 throws Exception { 50 HttpClient httpClient = wrapClient(host); 51 52 HttpGet request = new HttpGet(buildUrl(host, path, querys)); 53 for (Map.Entry<String, String> e : headers.entrySet()) { 54 request.addHeader(e.getKey(), e.getValue()); 55 } 56 57 return httpClient.execute(request); 58 } 59 60 /** 61 * post form 62 * 63 * @param host 64 * @param path 65 * @param method 66 * @param headers 67 * @param querys 68 * @param bodys 69 * @return 70 * @throws Exception 71 */ 72 public static HttpResponse doPost(String host, String path, String method, 73 Map<String, String> headers, 74 Map<String, String> querys, 75 Map<String, String> bodys) 76 throws Exception { 77 HttpClient httpClient = wrapClient(host); 78 79 HttpPost request = new HttpPost(buildUrl(host, path, querys)); 80 for (Map.Entry<String, String> e : headers.entrySet()) { 81 request.addHeader(e.getKey(), e.getValue()); 82 } 83 84 if (bodys != null) { 85 List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); 86 87 for (String key : bodys.keySet()) { 88 nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key))); 89 } 90 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8"); 91 formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); 92 request.setEntity(formEntity); 93 } 94 95 return httpClient.execute(request); 96 } 97 98 /** 99 * Post String 100 * 101 * @param host 102 * @param path 103 * @param method 104 * @param headers 105 * @param querys 106 * @param body 107 * @return 108 * @throws Exception 109 */ 110 public static HttpResponse doPost(String host, String path, String method, 111 Map<String, String> headers, 112 Map<String, String> querys, 113 String body) 114 throws Exception { 115 HttpClient httpClient = wrapClient(host); 116 117 HttpPost request = new HttpPost(buildUrl(host, path, querys)); 118 for (Map.Entry<String, String> e : headers.entrySet()) { 119 request.addHeader(e.getKey(), e.getValue()); 120 } 121 122 if (StringUtils.isNotBlank(body)) { 123 request.setEntity(new StringEntity(body, "utf-8")); 124 } 125 126 return httpClient.execute(request); 127 } 128 129 /** 130 * Post stream 131 * 132 * @param host 133 * @param path 134 * @param method 135 * @param headers 136 * @param querys 137 * @param body 138 * @return 139 * @throws Exception 140 */ 141 public static HttpResponse doPost(String host, String path, String method, 142 Map<String, String> headers, 143 Map<String, String> querys, 144 byte[] body) 145 throws Exception { 146 HttpClient httpClient = wrapClient(host); 147 148 HttpPost request = new HttpPost(buildUrl(host, path, querys)); 149 for (Map.Entry<String, String> e : headers.entrySet()) { 150 request.addHeader(e.getKey(), e.getValue()); 151 } 152 153 if (body != null) { 154 request.setEntity(new ByteArrayEntity(body)); 155 } 156 157 return httpClient.execute(request); 158 } 159 160 /** 161 * Put String 162 * @param host 163 * @param path 164 * @param method 165 * @param headers 166 * @param querys 167 * @param body 168 * @return 169 * @throws Exception 170 */ 171 public static HttpResponse doPut(String host, String path, String method, 172 Map<String, String> headers, 173 Map<String, String> querys, 174 String body) 175 throws Exception { 176 HttpClient httpClient = wrapClient(host); 177 178 HttpPut request = new HttpPut(buildUrl(host, path, querys)); 179 for (Map.Entry<String, String> e : headers.entrySet()) { 180 request.addHeader(e.getKey(), e.getValue()); 181 } 182 183 if (StringUtils.isNotBlank(body)) { 184 request.setEntity(new StringEntity(body, "utf-8")); 185 } 186 187 return httpClient.execute(request); 188 } 189 190 /** 191 * Put stream 192 * @param host 193 * @param path 194 * @param method 195 * @param headers 196 * @param querys 197 * @param body 198 * @return 199 * @throws Exception 200 */ 201 public static HttpResponse doPut(String host, String path, String method, 202 Map<String, String> headers, 203 Map<String, String> querys, 204 byte[] body) 205 throws Exception { 206 HttpClient httpClient = wrapClient(host); 207 208 HttpPut request = new HttpPut(buildUrl(host, path, querys)); 209 for (Map.Entry<String, String> e : headers.entrySet()) { 210 request.addHeader(e.getKey(), e.getValue()); 211 } 212 213 if (body != null) { 214 request.setEntity(new ByteArrayEntity(body)); 215 } 216 217 return httpClient.execute(request); 218 } 219 220 /** 221 * Delete 222 * 223 * @param host 224 * @param path 225 * @param method 226 * @param headers 227 * @param querys 228 * @return 229 * @throws Exception 230 */ 231 public static HttpResponse doDelete(String host, String path, String method, 232 Map<String, String> headers, 233 Map<String, String> querys) 234 throws Exception { 235 HttpClient httpClient = wrapClient(host); 236 237 HttpDelete request = new HttpDelete(buildUrl(host, path, querys)); 238 for (Map.Entry<String, String> e : headers.entrySet()) { 239 request.addHeader(e.getKey(), e.getValue()); 240 } 241 242 return httpClient.execute(request); 243 } 244 245 private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException { 246 StringBuilder sbUrl = new StringBuilder(); 247 sbUrl.append(host); 248 if (!StringUtils.isBlank(path)) { 249 sbUrl.append(path); 250 } 251 if (null != querys) { 252 StringBuilder sbQuery = new StringBuilder(); 253 for (Map.Entry<String, String> query : querys.entrySet()) { 254 if (0 < sbQuery.length()) { 255 sbQuery.append("&"); 256 } 257 if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) { 258 sbQuery.append(query.getValue()); 259 } 260 if (!StringUtils.isBlank(query.getKey())) { 261 sbQuery.append(query.getKey()); 262 if (!StringUtils.isBlank(query.getValue())) { 263 sbQuery.append("="); 264 sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8")); 265 } 266 } 267 } 268 if (0 < sbQuery.length()) { 269 sbUrl.append("?").append(sbQuery); 270 } 271 } 272 273 return sbUrl.toString(); 274 } 275 276 private static HttpClient wrapClient(String host) { 277 HttpClient httpClient = new DefaultHttpClient(); 278 if (host.startsWith("https://")) { 279 sslClient(httpClient); 280 } 281 282 return httpClient; 283 } 284 285 private static void sslClient(HttpClient httpClient) { 286 try { 287 SSLContext ctx = SSLContext.getInstance("TLS"); 288 X509TrustManager tm = new X509TrustManager() { 289 public X509Certificate[] getAcceptedIssuers() { 290 return null; 291 } 292 public void checkClientTrusted(X509Certificate[] xcs, String str) { 293 294 } 295 public void checkServerTrusted(X509Certificate[] xcs, String str) { 296 297 } 298 }; 299 ctx.init(null, new TrustManager[] { tm }, null); 300 SSLSocketFactory ssf = new SSLSocketFactory(ctx); 301 ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 302 ClientConnectionManager ccm = httpClient.getConnectionManager(); 303 SchemeRegistry registry = ccm.getSchemeRegistry(); 304 registry.register(new Scheme("https", 443, ssf)); 305 } catch (KeyManagementException ex) { 306 throw new RuntimeException(ex); 307 } catch (NoSuchAlgorithmException ex) { 308 throw new RuntimeException(ex); 309 } 310 } 311 }