1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 阿里云实名认证接口调试

阿里云实名认证接口调试

时间:2019-05-27 16:34:13

相关推荐

阿里云实名认证接口调试

今天项目里用到了阿里云的实名认证接口,具体如下:

1、身份证实名认证接口,用于验证身份证信息,具体代码如下:

1 String host = "http://telvertify."; 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 * /aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java18 * 下载19 *20 * 相应的依赖请参照21 * /aliyun/api-gateway-demo-sign-java/blob/master/pom.xml22 */23 HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);24 System.out.println(response.toString());25 //获取response的body26 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."; 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 * /aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java18 * 下载19 *20 * 相应的依赖请参照21 * /aliyun/api-gateway-demo-sign-java/blob/master/pom.xml22 */23 HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);24 System.out.println(response.toString());25 //获取response的body26 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 .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 .ssl.SSLContext; 12 import .ssl.TrustManager; 13 import .ssl.X509TrustManager; 14 15 import mons.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 { 3435/** 36* get 37* 38* @param host 39* @param path 40* @param method 41* @param headers 42* @param querys 43* @return 44* @throws Exception 45*/ 46public 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 } 5657 return httpClient.execute(request); 58} 5960/** 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*/ 72public 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}9798/** 99* Post String100* 101* @param host102* @param path103* @param method104* @param headers105* @param querys106* @param body107* @return108* @throws Exception109*/110public 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}128129/**130* Post stream131* 132* @param host133* @param path134* @param method135* @param headers136* @param querys137* @param body138* @return139* @throws Exception140*/141public 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}159160/**161* Put String162* @param host163* @param path164* @param method165* @param headers166* @param querys167* @param body168* @return169* @throws Exception170*/171public 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}189190/**191* Put stream192* @param host193* @param path194* @param method195* @param headers196* @param querys197* @param body198* @return199* @throws Exception200*/201public 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}219220/**221* Delete222* 223* @param host224* @param path225* @param method226* @param headers227* @param querys228* @return229* @throws Exception230*/231public 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}244245private 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())) {263sbQuery.append("=");264sbQuery.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}275276private static HttpClient wrapClient(String host) {277 HttpClient httpClient = new DefaultHttpClient();278 if (host.startsWith("https://")) {279 sslClient(httpClient);280 }281 282 return httpClient;283}284285private 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 }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。