-
Maven Dependence
<dependency><groupid>org.apache.httpcomponents</groupid><artifactid>httpclient</ Artifactid><version>4.5.2</version></dependency>
2. Test class
package com.iraid.test;import java.io.bufferedreader;import java.io.file;import java.io.fileinputstream;import java.io.ioexception;import java.io.inputstream;import java.io.inputstreamreader;import java.security.keystore;import javax.net.ssl.sslcontext;import org.apache.http.client.methods.closeablehttpresponse;import org.apache.http.client.methods.httppost; import org.apache.http.config.registry;import org.apache.http.config.registrybuilder;import org.apache.http.conn.httpclientconnectionmanager;import org.apache.http.conn.socket.connectionsocketfactory;import org.apache.http.conn.socket.plainconnectionsocketfactory;import Org.apache.http.conn.ssl.sslconnectionsocketfactory;import org.apache.http.conn.ssl.sslcontexts;import org.apache.http.conn.ssl.trustselfsignedstrategy;import org.apache.http.entity.contenttype;import org.apache.http.entity.stringentity;import org.apache.http.impl.client.closeablehttpclient;import org.apache.http.impl.client.httpclients;import org.apache.http.impl.conn.poolinghttpclientconnectionmanager;import org.apache.http.util.entityutils;/** * use httpclient4.5 for https communication, * with two-way authentication, connection pooling Management connection * * @author wangfeihu * */public class httpclientforssl {public static HttpClientConnectionManager CONNECTION_MANAGER = null;/** * initialization connection manager. * @param keyStoreFile * @param keyStorePass * @param trustStoreFile * @param trustStorePass * @throws exception */public void init (string keystorefile, string keystorepass,string Truststorefile, string truststorepass) throws exception {system.out.println ("Init conection pool ... "); INPUTSTREAM&NBsp;ksis = new fileinputstream (New file (keystorefile));inputstream tsis = New fileinputstream (New file (truststorefile)); Keystore ks = keystore.getinstance ("PKCS12"); Ks.load (Ksis, keystorepass.tochararray ()); Keystore ts = keystore.getinstance ("JKS"); Ts.load (Tsis, truststorepass.tochararray ()); Sslcontext sslcontext = sslcontexts.custom (). Loadkeymaterial (Ks, keystorepass.tochararray ( ))// If there is a server certificate. Loadtrustmaterial (Ts, new trustselfsignedstrategy ())// If there is no server certificate, You can use the custom trust mechanism// .loadtrustmaterial (Null, new truststrategy () {//// // Trust all// public boolean istrusted (X509CERTIFICATE[]&NBSP;ARG0,//&NBSP;STRING&NBSP;ARG1) throws certificateexception {// return true;// }//// }). build (); Sslconnectionsocketfactory sslsf = new sslconnectionsocketfactory (sslContext, new string[] { "TLSv1" }, null,sslconnectionsocketfactory.allow_all_hostname_verifier ); Registry<connectionsocketfactory> registry = registrybuilder.<connectionsocketfactory > create (). Register ("http", plainconnectionsocketfactory.instance). Register ("https", &NBSP;SSLSF). Build (); Ksis.close (); Tsis.close (); Connection_manager = new poolinghttpclientconnectionmanager (registry);} /** * do post * @param url * @param params * @throws exception */public void post (String url, string params) throws exception {if (Connection_manager == null) {return;} Closeablehttpclient httpclient = httpclients.custom (). Setconnectionmanager (CONNECTION_MANAGER). Build (); Httppost httppost = new httppost (URL); httppost.setentity (new stringentity (params, Contenttype.application_json)); CLoseablehttpresponse resp = httpclient.execute (HttpPost); System.out.println (Resp.getstatusline ()); Inputstream respis = resp.getentity (). getcontent (); String content = convertstreamtostring (Respis); SYSTEM.OUT.PRINTLN (content); Entityutils.consume (Resp.getentity ());} Public static string convertstreamtostring (Inputstream is) {BufferedReader Reader = new bufferedreader (New inputstreamreader (IS)); Stringbuilder sb = new stringbuilder (); string line = null;try {while ((Line = reader.readline ()) != null ) {sb.append (line + "\ n");}} catch (ioexception e) {e.printstacktrace ();} finally {try {is.close ();} catch (ioexception e) {e.printstacktrace ();}} Return sb.tostring ();} Public static void main (String[] args) {// service address string url = " Https://www.demo.com/api/rest/UidApiService/authCardWithoutOTP ";// service parameters, the parameters of the interface here are used json Format Pass string params = "{\" merchantcode\ ": \" www.demo.com\ "," + "\" sessionid\ ": \" 10000011\ "," + "\" username\ ": \" Jack\ "," + "\" idnumber\ ": \" 432652515\ "," + "\" cardno\ ": \" 561231321\ "," + "\" phoneno\ ": \" \ "}";// private key Certificate string keystorefile = "D:\\WORKSPACES\\TEST\\HTTPCLIENT\\SRC\\MAIN\\RESOURCES\\WWW.DEMO.COM.P12"; string keystorepass = "052537159932766";// Configure trust Certificate library and password string truststorefile = "D:\\workspaces\\test\\httpclient\\src\\main\\resources\\cacerts.jks"; string truststorepass = "Changeit"; Httpclientforssl obj = new httpclientforssl (); Try {obj.init (keyStoreFile, Keystorepass, truststorefile, truststorepass);for (int i = 0; i < 10; i++) {obj.post (url, params);}} catch (exception e) {e.printstacktrace ();}}
This article is from the "Wandering Footsteps" blog, please make sure to keep this source http://now51jq.blog.51cto.com/3474143/1790213
Apache httpclient-4.5 HTTPS Communication bidirectional authentication