Résumé: Récemment, il est nécessaire de fournir aux clients des interfaces API reposantes. QA utilise Postman pour les tests, mais l'interface de test de Postman est similaire mais pas la même chose que les appels Java. Je veux donc écrire un programme pour tester par moi-même l'interface API RESTful. Puisqu'il utilise HTTPS, je dois également considérer le traitement de HTTPS. Comme j'ai utilisé Java pour appeler l'interface RESTful pour la première fois, j'ai encore besoin de l'étudier, donc j'ai naturellement consulté certaines informations.
Analyse: Ce problème est différent des appels entre les modules. Par exemple, j'ai deux modules, frontal et arrière, le front-end fournit un affichage frontal, et le back-end fournit une prise en charge des données. J'ai utilisé Hession pour enregistrer les services fournis par Back End en tant que services à distance. À l'avant, ce service distant peut être directement ajusté à l'interface arrière via ce type de service distant. Mais ce n'est pas un problème pour une entreprise à utiliser lorsque son propre projet est fortement couplé. Cependant, si vous enregistrez un tel service à distance avec les clients, il ne semble pas bon et le couplage est trop élevé. J'envisage donc d'utiliser la méthode suivante.
1. HttpClient
Tout le monde est peut-être familier avec HttpClient mais ne le connaît pas. Il le connaît car il peut appeler à distance, par exemple, demander une URL, puis obtenir l'état de retour et les informations de retour dans la réponse. Cependant, aujourd'hui, j'en parle un peu plus, car le sujet d'aujourd'hui est HTTPS, ce qui implique le problème des certificats ou de l'authentification des utilisateurs.
Après avoir confirmé que l'utilisation de HTTPClient, j'ai recherché des informations pertinentes et j'ai constaté que la nouvelle version de HttpClient est différente de l'ancienne version et est compatible avec l'ancienne version, mais il n'est plus recommandé que l'ancienne version soit utilisée. De nombreuses méthodes ou classes ont été marquées comme obsolètes. Aujourd'hui, nous utiliserons l'ancienne version 4.2 et la dernière version 4.5.3 pour écrire le code respectivement.
Ancienne version 4.2
Besoin de certification
L'utilisation de l'authentification du certificat est sélectionnée pendant la phase de préparation du certificat
package com.darren.test.https.v42; Importer java.io.file; import java.io.fileInputStream; Importer java.security.keystore; import org.apache.http.conn.ssl.sslsocketfactory; La classe publique httpsCertifiedClient étend httpsClient {public httpscertifiedClient () {} @Override public void prepareCertificate () lève une exception {// Obtenez la bibliothèque de clé Keystore TrustStore = keystore.getInstance (keystore.getDefaultTore ()); FileInputStream Input = new FileInputStream (nouveau fichier ("c: /users/zhda6001/downloads/software/xxx.keystore")); // fileInputStream input = new FileInputStream (nouveau fichier ("c: /users/zhda6001/downloads/xxx.keystore")); // Mot de passe de la bibliothèque de clés TrustStore.load (Instream, "Mot de passe" .toCharArray ()); // Enregistrez la bibliothèque de clés this.socketfactory = new SSLSocketFactory (TrustStore); // ne vérifiez pas le nom de domaine socketfactory.sethostnameverifier (sslsocketfactory.allow_all_hostname_verifier); }}Sauter la certification
L'option de sauter l'authentification pendant la phase de préparation du certificat est de
package com.darren.test.https.v42; Importer java.security.cert.certificateException; Importer java.security.cert.x509Certificate; import javax.net.ssl.sslcontext; import javax.net.ssl.trustManager; Importer javax.net.ssl.x509TrustManager; import org.apache.http.conn.ssl.sslsocketfactory; La classe publique httpstrustClient étend httpsClient {public httpstrustClient () {} @Override public void prepareCertificate () lève l'exception {// skip Certificate Verification sslContext CTX = sslContext.getInstance ("tls"); X509TrustManager tm = new X509TrustManager () {@Override public void CheckClientTruted (x509Certificate [] Chain, String AuthType) lance CertificateException {} @Override public Void CheckServerTruted (x509Certificate [] chaîne, chaîne authtype) throws certificat X509Certificate [] getACceptEdISSUers () {return null; }}; // défini sur le certificat de confiance ctx.init (null, new TrustManager [] {tm}, null); // Mettez l'usine SSL Socket et définissez ne pas vérifier le nom d'hôte this.socketfactory = new SSLSocketFactory (CTX, sslsocketfactory.ALLOW_ALL_HOSTNAME_VERIFIER); }}Résumer
Maintenant, il est constaté que les deux classes héritent de la même classe httpsclient, et httpsClient hérite de la classe DefaulthTTPClient. On peut constater que le modèle de méthode de modèle est utilisé ici.
package com.darren.test.https.v42; import org.apache.http.conn.clientConnectionManager; import org.apache.http.conn.scheme.scheme; import org.apache.http.conn.ssl.sslsocketfactory; import org.apache.http.impl.client.defaulthTTPClient; La classe abstraite publique httpsClient étend DefaulthTTPClient {SSLSocketFactory SocketFactory protégé; / ** * Initialize httpsClient * * @return renvoie l'instance actuelle * @throws exception * / public httpsClient init () lève exception {this.prepareCertificate (); this.Regist (); retourner ceci; } / ** * Préparer la vérification du certificat * * @throws exception * / public abstract void prepareCertificate () lève une exception; / ** * Registre Protocole et port, cette méthode peut également être réécrite par sous-classes * / Protected void Regist () {ClientConnectionManager CCM = this.getConnectionManager (); SchemeRegistry sr = ccm.getschemeRegistry (); sr.register (nouveau schéma ("https", 443, socketfactory)); }} Vous trouverez ci-dessous la classe d'outils
package com.darren.test.https.v42; import java.util.arraylist; Importer java.util.list; importation java.util.map; import java.util.set; import org.apache.http.httpentity; import org.apache.http.httpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.urLencocedFormentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.client.methods.httprequestbase; import org.apache.http.message.basicNameValuepair; import org.apache.http.util.entityutils; classe publique httpsClientUtil {private static final String default_charset = "utf-8"; public static String doPost (httpsClient httpsClient, string url, map <string, string> paramheader, map <string, string> parambody) lève exception {return doPost (httpsClient, url, paramheader, parambody, default_charset); } public static String doPost (httpsClient httpsClient, string url, map <string, string> paramheader, map <string, string> parambody, string charset) lève exception {string result = null; HTTPPOST HTTPPOST = NOUVEAU HTTPPOST (URL); SETHEADER (HTTPPOST, PARAMEADER); setBody (httppost, parambody, charset); HttpResponse Response = httpsClient.Execute (httppost); if (Resentity! = null) {httpentity resentity = réponse.getEntity (); if (Resentity! = null) {result = entityUtils.tostring (Resentity, charset); }} Retour Résultat; } public static String doGet (httpsClient httpsClient, string url, map <string, string> paramheader, map <string, string> parambody) lève exception {return doget (httpsClient, url, paramheader, parambody, default_charset); } public static String doGet (httpsClient httpsClient, string url, map <string, string> paramheader, map <string, string> parambody, string charset) lève exception {string result = null; Httpget httpget = new httpget (url); Setheader (httpget, paramheader); HttpResponse Response = httpsClient.Execute (httpget); if (réponse! = null) {httpentity resentity = réponse.getEntity (); if (Resentity! = null) {result = entityUtils.tostring (Resentity, charset); }} Retour Résultat; } private static void sethader (requette httprequestbase, map <string, string> paramheader) {// set l'en-tête if (paramheader! = null) {set <string> keyset = paramheader.keyset (); for (String key: keyset) {request.addheader (key, paramheader.get (key)); }}} private static void setBody (httppost httppost, map <string, string> parambody, string charset) lève exception {// se définir le paramètre if (parambody! = null) {list <nameValuepair> list = new ArrayList <NameValuePair> (); Set <string> keyset = parambody.KeySet (); for (String key: keyset) {list.add (new BasicNameValuepair (key, parambody.get (key))); } if (list.size ()> 0) {UrLencodedFormentity Entity = new UrLenCodedFormentity (list, charset); httppost.sentity (entité); }}}} Ensuite, il y a la classe de test:
package com.darren.test.https.v42; import java.util.hashmap; importation java.util.map; classe publique httpsClientTest {public static void main (String [] args) lève l'exception {httpsClient httpsClient = null; httpsClient = new httpstrustClient (). init (); // httpsClient = new httpsCertifiedClient (). init (); String url = "https://1.2.6.2:8011/xxx/api/getToken"; // String url = "https://1.2.6.2:8011/xxx/api/geThealth"; Map <string, string> paramheader = new hashmap <> (); //paramheader.put("content-type "," application / json "); paramheader.put ("accepter", "application / xml"); Map <string, string> parambody = new hashmap <> (); parambody.put ("client_id", "[email protected]"); parambody.put ("client_secret", "p @ sword_1"); String result = httpsClientUtil.dopost (httpsClient, URL, paramheader, parambody); // Résultat de chaîne = httpsClientUtil.Doget (httpsClient, URL, null, null); System.out.println (résultat); }} Informations de retour:
<? xml version = "1.0" Encoding = "utf-8"?>
<Token> jkf8rl0Sw + skkflj8rBKI5HP1BEQK8PRCUTZPPBINQMYKRMXY1KWCJMCFT191ZPP88VV1AGHW8OYNWJEYS 0axplugax89ejcownbikcc1uvfyesxhlktcjqyufivjevhreqxjphnclqywp + xse5od9x8vkfkk7inntmrzqk7ybtz / e3u7gswm / 5cvahfl6o9req9cwpxavznohyvnxsohszdo + bxatxxa1xpedly / 8h / uap4n4dlzdjj3b8t1xh + crriom OPXF7C5WKHHTOKEOEXW + XOPQKKSX5CKWWJPUGIIFWF / PAQWG + JUOSVT7QGDPV8PMWJ9DWEWJTDXGUDG == </ Token>
Nouvelle version 4.5.3
Besoin de certification
package com.darren.test.https.v45; Importer java.io.file; import java.io.fileInputStream; Importer java.security.keystore; import javax.net.ssl.sslcontext; import org.apache.http.conn.ssl.sslconnectionsocketfactory; import org.apache.http.conn.ssl.sslconnectionsocketfactory; import org.apache.http.conn.ssl.trustSelfsignedStrategy; import org.apache.http.ssl.sslcontexts; La classe publique httpsCertifiedClient étend httpsClient {public httpscertifiedClient () {} @Override public void prepareCertificate () lève une exception {// Obtenez la bibliothèque de clé Keystore TrustStore = keystore.getInstance (keystore.getDefaultTore ()); FileInputStream InsideRam = new FileInputStream (nouveau fichier ("c: /users/zhda6001/downloads/software/xxx.keystore")); // fileInputStream InsideRam = new FileInputStream (nouveau fichier ("c: /users/zhda6001/downloads/xxx.keystore")); essayez {// mot de passe de la bibliothèque de clés TrustStore.Load (Instream, "Mot de passe" .toCharArray ()); } enfin {enterStream.close (); } SslContext sslContext = sslcontext.custom (). LoadTrustMaterial (TrustStore, TrustSelfSignedStrategy.instance) .build (); this.connectionsocketfactory = new sslConnectionSocketFactory (sslContext); }}Sauter la certification
package com.darren.test.https.v45; Importer java.security.cert.certificateException; Importer java.security.cert.x509Certificate; import javax.net.ssl.sslcontext; import javax.net.ssl.trustManager; Importer javax.net.ssl.x509TrustManager; import org.apache.http.conn.ssl.sslconnectionsocketfactory; La classe publique httpstrustClient étend httpsClient {public httpstrustClient () {} @Override public void prepareCertificate () lève l'exception {// skip Certificate Verification sslContext CTX = sslContext.getInstance ("tls"); X509TrustManager tm = new X509TrustManager () {@Override public void CheckClientTruted (x509Certificate [] Chain, String AuthType) lance CertificateException {} @Override public Void CheckServerTruted (x509Certificate [] chaîne, chaîne authtype) throws certificat X509Certificate [] getACceptEdISSUers () {return null; }}; // défini sur un certificat de confiance ctx.init (null, new TrustManager [] {tm}, null); this.connectionsocketfactory = new sslConnectionSocketFactory (CTX); }}Résumer
package com.darren.test.https.v45; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.connectionsocketfactory; import org.apache.http.conn.socket.plainConnectionSocketFactory; import org.apache.http.impl.client.closeablehttpclient; import org.apache.http.impl.client.httpclientBuilder; import org.apache.http.impl.client.httpclients; import org.apache.http.impl.conn.poolinghttpclientConnectionManager; La classe abstraite publique HttpsClient étend HttpClientBuilder {Client Private CloseableHttpClient; Connections protégées de Connections SocketFactory; / ** * Initialize httpsClient * * @return Renvoie l'instance actuelle * @throws exception * / public closablehttpclient init () lève exception {this.prepareCertificate (); this.Regist (); Renvoyez ceci.client; } / ** * Préparer la vérification du certificat * * @throws exception * / public abstract void prepareCertificate () lève une exception; / ** * Enregistrer le protocole et le port, cette méthode peut également être réécrite par sous-classes * / Protected void Regist () {// Définir l'objet correspondant à Protocole HTTP et HTTP PlainConnectionSocketFactory.Instance) .Register ("https", this.connectionSocketFactory) .Build (); PACLINGHTTPCLIENTCONNECTIONMANGER CONCNMANGER = NOUVEAU PACHORDHTTPCLIENTCONNECTIONMANGER (socketfactoryRegistry); HttpClients.Custom (). SetConnectionManager (ConnManager); // Créer un objet httpclient personnalisé this.client = httpclients.custom (). SetConnectionManager (ConnManager) .Build (); // CLOSEableHttpClient client = httpClients.CreateDefault (); }} Outils:
package com.darren.test.https.v45; import java.util.arraylist; Importer java.util.list; importation java.util.map; import java.util.set; import org.apache.http.httpentity; import org.apache.http.httpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urLencocedFormentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.client.methods.httprequestbase; import org.apache.http.message.basicNameValuepair; import org.apache.http.util.entityutils; classe publique httpsClientUtil {private static final String default_charset = "utf-8"; public static string doPost (httpclient httpclient, string url, map <string, string> paramheader, map <string, string> parambody) lève exception {return doPost (httpclient, url, paramheader, parambody, default_charset); } public static String doPost (httpclient httpclient, string url, map <string, string> paramheader, map <string, string> parambody, string charset) lève exception {string result = null; HTTPPOST HTTPPOST = NOUVEAU HTTPPOST (URL); SETHEADER (HTTPPOST, PARAMEADER); setBody (httppost, parambody, charset); HttpResponse Response = httpClient.Execute (httppost); if (Resentity! = null) {httpentity resentity = réponse.getEntity (); if (Resentity! = null) {result = entityUtils.tostring (Resentity, charset); }} Retour Résultat; } public static String doGet (httpclient httpclient, string url, map <string, string> paramheader, map <string, string> parambody) lève exception {return doget (httpclient, url, paramheader, parambody, default_charset); } public static String doGet (httpclient httpclient, string url, map <string, string> paramheader, map <string, string> parambody, string charset) lève exception {string result = null; Httpget httpget = new httpget (url); Setheader (httpget, paramheader); HttpResponse Response = httpClient.Execute (httpget); if (réponse! = null) {httpentity resentity = réponse.getEntity (); if (Resentity! = null) {result = entityUtils.tostring (Resentity, charset); }} Retour Résultat; } private static void sethader (requette httprequestbase, map <string, string> paramheader) {// set l'en-tête if (paramheader! = null) {set <string> keyset = paramheader.keyset (); for (String key: keyset) {request.addheader (key, paramheader.get (key)); }}} private static void setBody (httppost httppost, map <string, string> parambody, string charset) lève exception {// se définir le paramètre if (parambody! = null) {list <nameValuepair> list = new ArrayList <NameValuePair> (); Set <string> keyset = parambody.KeySet (); for (String key: keyset) {list.add (new BasicNameValuepair (key, parambody.get (key))); } if (list.size ()> 0) {UrLencodedFormentity Entity = new UrLenCodedFormentity (list, charset); httppost.sentity (entité); }}}}}} Classe de test:
package com.darren.test.https.v45; import java.util.hashmap; importation java.util.map; import org.apache.http.client.httpclient; classe publique httpsClientTest {public static void main (String [] args) lève l'exception {httpclient httpclient = null; // httpClient = new httpstrustClient (). init (); httpClient = new httpsCertifiedClient (). init (); String url = "https://1.2.6.2:8011/xxx/api/getToken"; // String url = "https://1.2.6.2:8011/xxx/api/geThealth"; Map <string, string> paramheader = new hashmap <> (); paramheader.put ("accepter", "application / xml"); Map <string, string> parambody = new hashmap <> (); parambody.put ("client_id", "[email protected]"); parambody.put ("client_secret", "p @ sword_1"); String result = httpsClientUtil.dopost (httpclient, url, paramheader, parambody); // Résultat de chaîne = httpsClientUtil.Doget (httpsClient, URL, null, null); System.out.println (résultat); }} résultat:
<? xml version = "1.0" Encoding = "utf-8"?>
<Token> rxitf9 // 7nxwxjs2cjijyhltvzunvmzxxeqtgn0u07sc9ysjeibpqte3hcjulskoxpeUyguveyi9jv7 / wi klrzxykc3ospatsm0kcbcKPHU0TB2CN / nfzv9fmlueowfbdyz + n0seii9k + 0gp7920dfencn17wujvmc0u2jwvm5fa jqkmilwodxz6a0dq + d7dqdjwvcwxbvj2ilhyib3pr805vppmi9atxrvako0oda006wejfofcgyg5p70wpj5rbl85v FY9WCVKD1R7J6NVJHXGH2GNIMHKJEJORMJDXW2GKIUSIWSELI / XPSWAO7 / CTWNWTNCTGK8PX2ZUB0ZFA == </ Token>
2. HttpurlConnection
3. Restemplate de Spring
D'autres méthodes seront complétées plus tard
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.