public class Httpclientobject {public org.apache.http.client.HttpClient httpclient4new;//= new HttpClient (); public STA Tic HttpClient buildhttpclient () {//Builder.setproxy (New Httphost ("127.0.0.1", 8888)); return Builder.build ();}// For httpclient, increase the bypass SSL certificate validation private static Poolinghttpclientconnectionmanager Newconnectionmanager () {Sslcontext Sslcontext = null; try {sslcontext = sslcontext.getinstance ("TLS"); X509trustmanager TM = new X509trustmanager () {public void checkclienttrusted (x509certificate[] chain, String authtype) th Rows certificateexception {} public void checkservertrusted (x509certificate[] chain, String authtype) throws Certificate Exception {} public x509certificate[] Getacceptedissuers () {return null;}}; Sslcontext.init (NULL, new trustmanager[] {TM}, NULL); registry<connectionsocketfactory> socketfactoryregistry = registrybuilder.<connectionsocketfactory> Create (). Register ("http", plainconnectionsocketfactory.instance). Register ("https", new SslcoNnectionsocketfactory (Sslcontext, Noophostnameverifier.instance)). Build (); Socketconfig config = Socketconfig.custom (). Setsotimeout (). build (); Poolinghttpclientconnectionmanager cm = new Poolinghttpclientconnectionmanager (socketfactoryregistry); Cm.setmaxtotal (20000); Cm.setdefaultmaxperroute (20000); Cm.setdefaultsocketconfig (config); return cm; } catch (Exception E1) {e1.printstacktrace ();} return null; } private static Poolinghttpclientconnectionmanager POOLCM = null; static {POOLCM = Newconnectionmanager (); new Idleconnectionmonitorthread (POOLCM). Start ();} private static Final Request Config defaultrequestconfig = Requestconfig.custom (). Setconnecttimeout (+). Setconnectionrequesttimeout (1000 * (setsockettimeout). build (); private static final Httpclientbuilder builder = Httpclients.custom (). Setconnectionmanager (POOLCM). Setdefaultrequestconfig (Defaultrequestconfig); /** * Timed cleanup of failed connections in connection pool */private static class Idleconnectionmonitorthread extends Thread {private final Httpclientconnectionmanager connmgr; private volatile boolean shutdown; public Idleconnectionmonitorthre AD (Httpclientconnectionmanager connmgr) {super (); this.connmgr = Connmgr; This.setdaemon (true);} @Override public void R Un () {try {while (!shutdown) {synchronized (this) {wait (n);//Close Expired Connections Connmgr.closeexpiredconnec tions (); Optionally, close connections//That has been idle longer than SEC connmgr.closeidleconnections (Timeunit.secon DS); }}} catch (Interruptedexception ex) {//Terminate}}}}
Httpclient4 instructions for use