Credential class Interface Java code example

Source: Internet
Author: User

Often encounter docking data interface, actually this physical life

1. List of supported document types
Request Address: http://v.juhe.cn/certificates/typeList.php?key= you apply for Appkey
This interface can be obtained through GET request results, Java Network request has httpclient related toolkit and httpurlconnection related packages, etc., here is httpclient, need the pilot package, if using MAVEN will be more convenient to directly put:
1
org.apache.httpcomponentshttpmime4.3.6org.apache.httpcomponentshttpclient4.4.1

Copy to configuration file Pom.xml
The request code is as follows:

public static String get () throws IOException {
Create a HttpClient object
Closeablehttpclient httpClient = Httpclients.createdefault ();
Closeablehttpresponse response = null;
String result = null;
try {
HttpGet httpget = new HttpGet ("http://v.juhe.cn/certificates/typeList.php?key=" + AppKey);
Perform network requests
Response = Httpclient.execute (HttpGet);
GET request Entity
Httpentity resentity = response.getentity ();
if (resentity! = null) {
Converstreamtostring is one of the ways to convert a network-requested byte stream to a UTF8 string
result = Convertstreamtostring (Resentity.getcontent (), "UTF-8");
}
Entityutils.consume (resentity);
} catch (Exception e) {
} finally {
Close Request
Response.close ();
Httpclient.close ();
}
The resulting JSON-type data requires a third-party parsing of the JSON jar package to parse
return result;
}
2. Identification of photo ID
Request Address: http://v.juhe.cn/certificates/query.php

This method is a POST request upload parameter that contains the local picture information file type
public static string post (string type, file file) throws Exception {
Closeablehttpclient httpClient = Httpclients.createdefault ();
Closeablehttpresponse response = null;
String result = null;
HttpClient the relevant settings of the request, you can set the connection and timeout length (ms) with default parameters, without configuration.
Requestconfig config = Requestconfig.custom (). Setconnecttimeout (30000). SetSocketTimeout (30000). Build ();
try {
HttpPost HttpPost = new HttpPost ("http://v.juhe.cn/certificates/query.php");
Filebody encapsulating the parameters of the file type
Filebody bin = new Filebody (file);
Stringbody encapsulates a parameter of type string
Stringbody keybody = new Stringbody (key, Contenttype.text_plain);
Stringbody typebody = new Stringbody (type, contenttype.text_plain);
Addpart Pass the argument and specify the parameter name
Httpentity reqentity = Multipartentitybuilder.create ()
. Addpart ("Pic", bin). Addpart ("key", Keybody)
. Addpart ("Cardtype", Typebody). Build ();
Httppost.setentity (reqentity);
Httppost.setconfig (config);
Perform network requests and return results
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 ();
}
The resulting JSON-type data requires a third-party parsing of the JSON jar package to parse
return result;
}

This method is to transfer the bytes passed into the corresponding string and return, this method is generally used in network requests
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 ();
}

Credential class Interface Java code example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.