1. 程式人生 > >使用阿裏雲接口進行銀行卡四要素實名認證

使用阿裏雲接口進行銀行卡四要素實名認證

exe 一致性 man 上傳 stringbu 具體實現 推薦 tls throws

  如今隨著互聯網產業的多元化發展,尤其是互聯網金融,O2O,共享經濟等新興商業形式的興起,企業對實名認證業務的數據形式和數據質量有了更高的需求。如今也衍生出銀行卡實名認證業務,通過接口將銀行卡號、手機號、身份證號碼、姓名上傳至阿裏雲,再與銀行系統進行匹配,判斷信息的一致性。

在使用接口服務的方面我推薦使用技術實力強大的阿裏雲;

首先點擊:(阿裏雲API接口)獲取相應的訂單後在控制臺中可以得到您的appcode;

具體實現類:

 1 import java.util.HashMap;
 2 import java.util.Map;
 3 
 4 import org.apache.http.HttpResponse;
5 import org.apache.http.util.EntityUtils; 6 7 import com.netgate.util.send.HttpUtils; 8 9 public class AlipayBankNoCheck { 10 11 public static void main(String[] args) { 12 String host = "https://yunyidata.market.alicloudapi.com"; 13 String path = "/bankAuthenticate4"; 14 String method = "POST";
15 String appcode = "你的appcode"; 16 Map<String, String> headers = new HashMap<String, String>(); 17 //最後在header中的格式(中間是英文空格)為Authorization:APPCODE 83359fd73fe94948385f570e3c139105 18 headers.put("Authorization", "APPCODE " + appcode); 19 //根據API的要求,定義相對應的Content-Type
20 headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 21 Map<String, String> querys = new HashMap<String, String>(); 22 Map<String, String> bodys = new HashMap<String, String>(); 23 bodys.put("cardNo", "621555888555222669"); 24 bodys.put("idNo", "3404251111122222255555"); 25 bodys.put("name", "張三"); 26 bodys.put("phoneNo", "13355558888"); 27 28 try { 29 /** 30 * 重要提示如下: 31 * HttpUtils請從 32 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java 33 * 下載 34 * 35 * 相應的依賴請參照 36 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml 37 */ 38 HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); 39 System.out.println(response.toString()); 40 //獲取response的body 41 System.out.println(EntityUtils.toString(response.getEntity())); 42 } catch (Exception e) { 43 e.printStackTrace(); 44 } 45 } 46 47 }

工具類HttpUtils:

  1 import java.io.UnsupportedEncodingException;
  2 import java.net.URLEncoder;
  3 import java.security.KeyManagementException;
  4 import java.security.NoSuchAlgorithmException;
  5 import java.security.cert.X509Certificate;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 import java.util.Map;
  9 
 10 import javax.net.ssl.SSLContext;
 11 import javax.net.ssl.TrustManager;
 12 import javax.net.ssl.X509TrustManager;
 13 
 14 import org.apache.commons.lang.StringUtils;
 15 import org.apache.http.HttpResponse;
 16 import org.apache.http.NameValuePair;
 17 import org.apache.http.client.HttpClient;
 18 import org.apache.http.client.entity.UrlEncodedFormEntity;
 19 import org.apache.http.client.methods.HttpDelete;
 20 import org.apache.http.client.methods.HttpGet;
 21 import org.apache.http.client.methods.HttpPost;
 22 import org.apache.http.client.methods.HttpPut;
 23 import org.apache.http.conn.ClientConnectionManager;
 24 import org.apache.http.conn.scheme.Scheme;
 25 import org.apache.http.conn.scheme.SchemeRegistry;
 26 import org.apache.http.conn.ssl.SSLSocketFactory;
 27 import org.apache.http.entity.ByteArrayEntity;
 28 import org.apache.http.entity.StringEntity;
 29 import org.apache.http.impl.client.DefaultHttpClient;
 30 import org.apache.http.message.BasicNameValuePair;
 31 
 32 public class HttpUtils {
 33     
 34     /**
 35      * get
 36      * 
 37      * @param host
 38      * @param path
 39      * @param method
 40      * @param headers
 41      * @param querys
 42      * @return
 43      * @throws Exception
 44      */
 45     public static HttpResponse doGet(String host, String path, String method, 
 46             Map<String, String> headers, 
 47             Map<String, String> querys)
 48             throws Exception {        
 49         HttpClient httpClient = wrapClient(host);
 50 
 51         HttpGet request = new HttpGet(buildUrl(host, path, querys));
 52         for (Map.Entry<String, String> e : headers.entrySet()) {
 53             request.addHeader(e.getKey(), e.getValue());
 54         }
 55         
 56         return httpClient.execute(request);
 57     }
 58     
 59     /**
 60      * post form
 61      * 
 62      * @param host
 63      * @param path
 64      * @param method
 65      * @param headers
 66      * @param querys
 67      * @param bodys
 68      * @return
 69      * @throws Exception
 70      */
 71     public static HttpResponse doPost(String host, String path, String method, 
 72             Map<String, String> headers, 
 73             Map<String, String> querys, 
 74             Map<String, String> bodys)
 75             throws Exception {        
 76         HttpClient httpClient = wrapClient(host);
 77 
 78         HttpPost request = new HttpPost(buildUrl(host, path, querys));
 79         for (Map.Entry<String, String> e : headers.entrySet()) {
 80             request.addHeader(e.getKey(), e.getValue());
 81         }
 82 
 83         if (bodys != null) {
 84             List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
 85 
 86             for (String key : bodys.keySet()) {
 87                 nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
 88             }
 89             UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
 90             formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
 91             request.setEntity(formEntity);
 92         }
 93 
 94         return httpClient.execute(request);
 95     }    
 96     
 97     /**
 98      * Post String
 99      * 
100      * @param host
101      * @param path
102      * @param method
103      * @param headers
104      * @param querys
105      * @param body
106      * @return
107      * @throws Exception
108      */
109     public static HttpResponse doPost(String host, String path, String method, 
110             Map<String, String> headers, 
111             Map<String, String> querys, 
112             String body)
113             throws Exception {        
114         HttpClient httpClient = wrapClient(host);
115 
116         HttpPost request = new HttpPost(buildUrl(host, path, querys));
117         for (Map.Entry<String, String> e : headers.entrySet()) {
118             request.addHeader(e.getKey(), e.getValue());
119         }
120 
121         if (StringUtils.isNotBlank(body)) {
122             request.setEntity(new StringEntity(body, "utf-8"));
123         }
124 
125         return httpClient.execute(request);
126     }
127     
128     /**
129      * Post stream
130      * 
131      * @param host
132      * @param path
133      * @param method
134      * @param headers
135      * @param querys
136      * @param body
137      * @return
138      * @throws Exception
139      */
140     public static HttpResponse doPost(String host, String path, String method, 
141             Map<String, String> headers, 
142             Map<String, String> querys, 
143             byte[] body)
144             throws Exception {        
145         HttpClient httpClient = wrapClient(host);
146 
147         HttpPost request = new HttpPost(buildUrl(host, path, querys));
148         for (Map.Entry<String, String> e : headers.entrySet()) {
149             request.addHeader(e.getKey(), e.getValue());
150         }
151 
152         if (body != null) {
153             request.setEntity(new ByteArrayEntity(body));
154         }
155 
156         return httpClient.execute(request);
157     }
158     
159     /**
160      * Put String
161      * @param host
162      * @param path
163      * @param method
164      * @param headers
165      * @param querys
166      * @param body
167      * @return
168      * @throws Exception
169      */
170     public static HttpResponse doPut(String host, String path, String method, 
171             Map<String, String> headers, 
172             Map<String, String> querys, 
173             String body)
174             throws Exception {        
175         HttpClient httpClient = wrapClient(host);
176 
177         HttpPut request = new HttpPut(buildUrl(host, path, querys));
178         for (Map.Entry<String, String> e : headers.entrySet()) {
179             request.addHeader(e.getKey(), e.getValue());
180         }
181 
182         if (StringUtils.isNotBlank(body)) {
183             request.setEntity(new StringEntity(body, "utf-8"));
184         }
185 
186         return httpClient.execute(request);
187     }
188     
189     /**
190      * Put stream
191      * @param host
192      * @param path
193      * @param method
194      * @param headers
195      * @param querys
196      * @param body
197      * @return
198      * @throws Exception
199      */
200     public static HttpResponse doPut(String host, String path, String method, 
201             Map<String, String> headers, 
202             Map<String, String> querys, 
203             byte[] body)
204             throws Exception {        
205         HttpClient httpClient = wrapClient(host);
206 
207         HttpPut request = new HttpPut(buildUrl(host, path, querys));
208         for (Map.Entry<String, String> e : headers.entrySet()) {
209             request.addHeader(e.getKey(), e.getValue());
210         }
211 
212         if (body != null) {
213             request.setEntity(new ByteArrayEntity(body));
214         }
215 
216         return httpClient.execute(request);
217     }
218     
219     /**
220      * Delete
221      *  
222      * @param host
223      * @param path
224      * @param method
225      * @param headers
226      * @param querys
227      * @return
228      * @throws Exception
229      */
230     public static HttpResponse doDelete(String host, String path, String method, 
231             Map<String, String> headers, 
232             Map<String, String> querys)
233             throws Exception {        
234         HttpClient httpClient = wrapClient(host);
235 
236         HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
237         for (Map.Entry<String, String> e : headers.entrySet()) {
238             request.addHeader(e.getKey(), e.getValue());
239         }
240         
241         return httpClient.execute(request);
242     }
243     
244     private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
245         StringBuilder sbUrl = new StringBuilder();
246         sbUrl.append(host);
247         if (!StringUtils.isBlank(path)) {
248             sbUrl.append(path);
249         }
250         if (null != querys) {
251             StringBuilder sbQuery = new StringBuilder();
252             for (Map.Entry<String, String> query : querys.entrySet()) {
253                 if (0 < sbQuery.length()) {
254                     sbQuery.append("&");
255                 }
256                 if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
257                     sbQuery.append(query.getValue());
258                 }
259                 if (!StringUtils.isBlank(query.getKey())) {
260                     sbQuery.append(query.getKey());
261                     if (!StringUtils.isBlank(query.getValue())) {
262                         sbQuery.append("=");
263                         sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
264                     }                    
265                 }
266             }
267             if (0 < sbQuery.length()) {
268                 sbUrl.append("?").append(sbQuery);
269             }
270         }
271         
272         return sbUrl.toString();
273     }
274     
275     private static HttpClient wrapClient(String host) {
276         HttpClient httpClient = new DefaultHttpClient();
277         if (host.startsWith("https://")) {
278             sslClient(httpClient);
279         }
280         
281         return httpClient;
282     }
283     
284     private static void sslClient(HttpClient httpClient) {
285         try {
286             SSLContext ctx = SSLContext.getInstance("TLS");
287             X509TrustManager tm = new X509TrustManager() {
288                 public X509Certificate[] getAcceptedIssuers() {
289                     return null;
290                 }
291                 public void checkClientTrusted(X509Certificate[] xcs, String str) {
292                     
293                 }
294                 public void checkServerTrusted(X509Certificate[] xcs, String str) {
295                     
296                 }
297             };
298             ctx.init(null, new TrustManager[] { tm }, null);
299             SSLSocketFactory ssf = new SSLSocketFactory(ctx);
300             ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
301             ClientConnectionManager ccm = httpClient.getConnectionManager();
302             SchemeRegistry registry = ccm.getSchemeRegistry();
303             registry.register(new Scheme("https", 443, ssf));
304         } catch (KeyManagementException ex) {
305             throw new RuntimeException(ex);
306         } catch (NoSuchAlgorithmException ex) {
307             throw new RuntimeException(ex);
308         }
309     }
310 }

使用阿裏雲接口進行銀行卡四要素實名認證