HTTPCLIENT4 Instructions for use

Source: Internet
Author: User
Tags ssl certificate

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

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.