證件類介面java代碼執行個體

來源:互聯網
上載者:User

標籤:reader   相關   typelist   string類   地圖   執行   pcl   識別   maven   

常有遇到對接資料介面,其實這個體力活而已

1.支援的證件類型清單
請求地址:http://v.juhe.cn/certificates/typeList.php?key=您申請的appkey
此介面可通過GET請求得到結果,java網路請求有HttpClient相關工具包及HttpURLConnection相關的包等,這裡用的是HttpClient,需要先導包,如果用maven話會更方便直接把:
1
org.apache.httpcomponentshttpmime4.3.6org.apache.httpcomponentshttpclient4.4.1

複製到設定檔 pom.xml
請求代碼如下:

public static String get() throws IOException {
// 建立HttpClient對象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String result = null;
try {
HttpGet httpGet = new HttpGet("http://v.juhe.cn/certificates/typeList.php?key="+ appKey);
// 執行網路請求
response = httpClient.execute(httpGet);
// 擷取請求實體
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
// ConverStreamToString是下面寫的一個方法是把網路請求的位元組流轉換為utf8的字串
result = ConvertStreamToString(resEntity.getContent(), "UTF-8");
}
EntityUtils.consume(resEntity);
} catch (Exception e) {
} finally {
// 關閉請求
response.close();
httpClient.close();
}
// 得到的是JSON類型的資料需要第三方解析JSON的jar包來解析
return result;
}
2.證件圖片識別
請求地址:http://v.juhe.cn/certificates/query.php

// 此方法是POST請求上傳的參數中包含本地圖片資訊File類型
public static String post(String type, File file) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String result = null;
// HttpClient請求的相關設定,可以不用配置,用預設的參數,這裡設定串連和逾時時間長度(毫秒)
RequestConfig config = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build();
try {
HttpPost httppost = new HttpPost("http://v.juhe.cn/certificates/query.php");
// FileBody封裝File類型的參數
FileBody bin = new FileBody(file);
// StringBody封裝String類型的參數
StringBody keyBody = new StringBody(key, ContentType.TEXT_PLAIN);
StringBody typeBody = new StringBody(type, ContentType.TEXT_PLAIN);
// addPart將參數傳入,並指定參數名稱
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("pic", bin).addPart("key", keyBody)
.addPart("cardType", typeBody).build();
httppost.setEntity(reqEntity);
httppost.setConfig(config);
// 執行網路請求並返回結果
response = httpClient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = ConvertStreamToString(resEntity.getContent(), "UTF-8");
}
EntityUtils.consume(resEntity);
} finally {
response.close();
httpClient.close();
}
// 得到的是JSON類型的資料需要第三方解析JSON的jar包來解析
return result;
}

// 此方法是把傳進的位元組流轉化為相應的字串並返回,此方法一般在網路請求中用到
public static String ConvertStreamToString(InputStream is, String charset)
throws Exception {
StringBuilder sb = new StringBuilder();
try (InputStreamReader inputStreamReader = new InputStreamReader(is,charset)) {
try (BufferedReader reader = new BufferedReader(inputStreamReader)) {
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\r\n");
}
}
}
return sb.toString();
}

 

證件類介面java代碼執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.