如今随着互联网产业的多元化发展,尤其是互联网金融、O2O、共享经济等新兴商业形式的兴起,企业对实名认证业务的数据形式和数据质量有了更高的需求。如今也衍生出手机号实名认证业务,通过接口将手机号、身份证号码、姓名上传至阿里云,再与运营商系统进行匹配,判断信息的一致性。
在使用接口服务的方面我推荐使用技术实力强大的阿里云;
首先点击:(阿里云API接口)获取相应的订单后在控制台中可以得到您的appcode;
具体实现类:
1 public static void main(String[] args) {
2 String host = "https://phonecheck.market.alicloudapi.com";
3 String path = "/phoneAuthentication";
4 String method = "POST";
5 String appcode = "你自己的AppCode";
6 Map<String, String> headers = new HashMap<String, String>();
7 //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
8 headers.put("Authorization", "APPCODE " + appcode);
9 //根据API的要求,定义相对应的Content-Type
10 headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
11 Map<String, String> querys = new HashMap<String, String>();
12 Map<String, String> bodys = new HashMap<String, String>();
13 bodys.put("idNo", "350298189012083221");
14 bodys.put("name", "张三");
15 bodys.put("phoneNo", "13511112222");
16
17
18 try {
19 /**
20 * 重要提示如下:
21 * HttpUtils请从
22 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
23 * 下载
24 *
25 * 相应的依赖请参照
26 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
27 */
28 HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
29 System.out.println(response.toString());
30 //获取response的body
31 //System.out.println(EntityUtils.toString(response.getEntity()));
32 } catch (Exception e) {
33 e.printStackTrace();
34 }
35 }
工具类HttpUtils:
1 package com.netgate.util.send;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5 import java.security.KeyManagementException;
6 import java.security.NoSuchAlgorithmException;
7 import java.security.cert.X509Certificate;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Map;
11
12 import javax.net.ssl.SSLContext;
13 import javax.net.ssl.TrustManager;
14 import javax.net.ssl.X509TrustManager;
15
16 import org.apache.commons.lang.StringUtils;
17 import org.apache.http.HttpResponse;
18 import org.apache.http.NameValuePair;
19 import org.apache.http.client.HttpClient;
20 import org.apache.http.client.entity.UrlEncodedFormEntity;
21 import org.apache.http.client.methods.HttpDelete;
22 import org.apache.http.client.methods.HttpGet;
23 import org.apache.http.client.methods.HttpPost;
24 import org.apache.http.client.methods.HttpPut;
25 import org.apache.http.conn.ClientConnectionManager;
26 import org.apache.http.conn.scheme.Scheme;
27 import org.apache.http.conn.scheme.SchemeRegistry;
28 import org.apache.http.conn.ssl.SSLSocketFactory;
29 import org.apache.http.entity.ByteArrayEntity;
30 import org.apache.http.entity.StringEntity;
31 import org.apache.http.impl.client.DefaultHttpClient;
32 import org.apache.http.message.BasicNameValuePair;
33
34 public class HttpUtils {
35
36 /**
37 * get
38 *
39 * @param host
40 * @param path
41 * @param method
42 * @param headers
43 * @param querys
44 * @return
45 * @throws Exception
46 */
47 public static HttpResponse doGet(String host, String path, String method,
48 Map<String, String> headers,
49 Map<String, String> querys)
50 throws Exception {
51 HttpClient httpClient = wrapClient(host);
52
53 HttpGet request = new HttpGet(buildUrl(host, path, querys));
54 for (Map.Entry<String, String> e : headers.entrySet()) {
55 request.addHeader(e.getKey(), e.getValue());
56 }
57
58 return httpClient.execute(request);
59 }
60
61 /**
62 * post form
63 *
64 * @param host
65 * @param path
66 * @param method
67 * @param headers
68 * @param querys
69 * @param bodys
70 * @return
71 * @throws Exception
72 */
73 public static HttpResponse doPost(String host, String path, String method,
74 Map<String, String> headers,
75 Map<String, String> querys,
76 Map<String, String> bodys)
77 throws Exception {
78 HttpClient httpClient = wrapClient(host);
79
80 HttpPost request = new HttpPost(buildUrl(host, path, querys));
81 for (Map.Entry<String, String> e : headers.entrySet()) {
82 request.addHeader(e.getKey(), e.getValue());
83 }
84
85 if (bodys != null) {
86 List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
87
88 for (String key : bodys.keySet()) {
89 nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
90 }
91 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
92 formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
93 request.setEntity(formEntity);
94 }
95
96 return httpClient.execute(request);
97 }
98
99 /**
100 * Post String
101 *
102 * @param host
103 * @param path
104 * @param method
105 * @param headers
106 * @param querys
107 * @param body
108 * @return
109 * @throws Exception
110 */
111 public static HttpResponse doPost(String host, String path, String method,
112 Map<String, String> headers,
113 Map<String, String> querys,
114 String body)
115 throws Exception {
116 HttpClient httpClient = wrapClient(host);
117
118 HttpPost request = new HttpPost(buildUrl(host, path, querys));
119 for (Map.Entry<String, String> e : headers.entrySet()) {
120 request.addHeader(e.getKey(), e.getValue());
121 }
122
123 if (StringUtils.isNotBlank(body)) {
124 request.setEntity(new StringEntity(body, "utf-8"));
125 }
126
127 return httpClient.execute(request);
128 }
129
130 /**
131 * Post stream
132 *
133 * @param host
134 * @param path
135 * @param method
136 * @param headers
137 * @param querys
138 * @param body
139 * @return
140 * @throws Exception
141 */
142 public static HttpResponse doPost(String host, String path, String method,
143 Map<String, String> headers,
144 Map<String, String> querys,
145 byte[] body)
146 throws Exception {
147 HttpClient httpClient = wrapClient(host);
148
149 HttpPost request = new HttpPost(buildUrl(host, path, querys));
150 for (Map.Entry<String, String> e : headers.entrySet()) {
151 request.addHeader(e.getKey(), e.getValue());
152 }
153
154 if (body != null) {
155 request.setEntity(new ByteArrayEntity(body));
156 }
157
158 return httpClient.execute(request);
159 }
160
161 /**
162 * Put String
163 * @param host
164 * @param path
165 * @param method
166 * @param headers
167 * @param querys
168 * @param body
169 * @return
170 * @throws Exception
171 */
172 public static HttpResponse doPut(String host, String path, String method,
173 Map<String, String> headers,
174 Map<String, String> querys,
175 String body)
176 throws Exception {
177 HttpClient httpClient = wrapClient(host);
178
179 HttpPut request = new HttpPut(buildUrl(host, path, querys));
180 for (Map.Entry<String, String> e : headers.entrySet()) {
181 request.addHeader(e.getKey(), e.getValue());
182 }
183
184 if (StringUtils.isNotBlank(body)) {
185 request.setEntity(new StringEntity(body, "utf-8"));
186 }
187
188 return httpClient.execute(request);
189 }
190
191 /**
192 * Put stream
193 * @param host
194 * @param path
195 * @param method
196 * @param headers
197 * @param querys
198 * @param body
199 * @return
200 * @throws Exception
201 */
202 public static HttpResponse doPut(String host, String path, String method,
203 Map<String, String> headers,
204 Map<String, String> querys,
205 byte[] body)
206 throws Exception {
207 HttpClient httpClient = wrapClient(host);
208
209 HttpPut request = new HttpPut(buildUrl(host, path, querys));
210 for (Map.Entry<String, String> e : headers.entrySet()) {
211 request.addHeader(e.getKey(), e.getValue());
212 }
213
214 if (body != null) {
215 request.setEntity(new ByteArrayEntity(body));
216 }
217
218 return httpClient.execute(request);
219 }
220
221 /**
222 * Delete
223 *
224 * @param host
225 * @param path
226 * @param method
227 * @param headers
228 * @param querys
229 * @return
230 * @throws Exception
231 */
232 public static HttpResponse doDelete(String host, String path, String method,
233 Map<String, String> headers,
234 Map<String, String> querys)
235 throws Exception {
236 HttpClient httpClient = wrapClient(host);
237
238 HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
239 for (Map.Entry<String, String> e : headers.entrySet()) {
240 request.addHeader(e.getKey(), e.getValue());
241 }
242
243 return httpClient.execute(request);
244 }
245
246 private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
247 StringBuilder sbUrl = new StringBuilder();
248 sbUrl.append(host);
249 if (!StringUtils.isBlank(path)) {
250 sbUrl.append(path);
251 }
252 if (null != querys) {
253 StringBuilder sbQuery = new StringBuilder();
254 for (Map.Entry<String, String> query : querys.entrySet()) {
255 if (0 < sbQuery.length()) {
256 sbQuery.append("&");
257 }
258 if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
259 sbQuery.append(query.getValue());
260 }
261 if (!StringUtils.isBlank(query.getKey())) {
262 sbQuery.append(query.getKey());
263 if (!StringUtils.isBlank(query.getValue())) {
264 sbQuery.append("=");
265 sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
266 }
267 }
268 }
269 if (0 < sbQuery.length()) {
270 sbUrl.append("?").append(sbQuery);
271 }
272 }
273
274 return sbUrl.toString();
275 }
276
277 private static HttpClient wrapClient(String host) {
278 HttpClient httpClient = new DefaultHttpClient();
279 if (host.startsWith("https://")) {
280 sslClient(httpClient);
281 }
282
283 return httpClient;
284 }
285
286 private static void sslClient(HttpClient httpClient) {
287 try {
288 SSLContext ctx = SSLContext.getInstance("TLS");
289 X509TrustManager tm = new X509TrustManager() {
290 public X509Certificate[] getAcceptedIssuers() {
291 return null;
292 }
293 public void checkClientTrusted(X509Certificate[] xcs, String str) {
294
295 }
296 public void checkServerTrusted(X509Certificate[] xcs, String str) {
297
298 }
299 };
300 ctx.init(null, new TrustManager[] { tm }, null);
301 SSLSocketFactory ssf = new SSLSocketFactory(ctx);
302 ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
303 ClientConnectionManager ccm = httpClient.getConnectionManager();
304 SchemeRegistry registry = ccm.getSchemeRegistry();
305 registry.register(new Scheme("https", 443, ssf));
306 } catch (KeyManagementException ex) {
307 throw new RuntimeException(ex);
308 } catch (NoSuchAlgorithmException ex) {
309 throw new RuntimeException(ex);
310 }
311 }
312 }