Package Com.cycore.dbs.common.demo.hc;import Java.io.ioexception;import Java.net.inetaddress;import Java.net.inetsocketaddress;import Java.net.proxyselector;import Java.net.socket;import Java.security.KeyStore; Import Java.util.concurrent.executionexception;import Java.util.concurrent.timeunit;import Javax.net.ssl.sslcontext;import Org.apache.http.headerelement;import Org.apache.http.HeaderElementIterator; Import Org.apache.http.httpclientconnection;import Org.apache.http.httpexception;import org.apache.http.HttpHost; Import Org.apache.http.httprequest;import Org.apache.http.httpresponse;import Org.apache.http.client.methods.httpget;import Org.apache.http.client.protocol.httpclientcontext;import Org.apache.http.config.registry;import Org.apache.http.config.registrybuilder;import Org.apache.http.conn.connectionkeepalivestrategy;import Org.apache.http.conn.connectionrequest;import Org.apache.http.conn.httpclientconnectionmanager;import Org.apache.http.conn.routing.httproute;import Org.apachE.http.conn.routing.httprouteplanner;import Org.apache.http.conn.socket.connectionsocketfactory;import Org.apache.http.conn.socket.layeredconnectionsocketfactory;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.impl.client.closeablehttpclient;import Org.apache.http.impl.client.httpclients;import Org.apache.http.impl.conn.basichttpclientconnectionmanager;import Org.apache.http.impl.conn.defaultproxyrouteplanner;import Org.apache.http.impl.conn.poolinghttpclientconnectionmanager;import Org.apache.http.impl.conn.systemdefaultrouteplanner;import Org.apache.http.message.BasicHeaderElementIterator; Import Org.apache.http.protocol.http;import Org.apache.http.protocol.httpcontext;public class HC_ connectionmanagement {/** * Manage HTTP connection and Connection Manager * @throws interruptedexception * @throws executionexception * @throws Ioexcepti On */public void M1 () throws InterruptedeXception, Executionexception, Ioexception{httpclientcontext context=httpclientcontext.create ();// Basichttpclientconnectionmanager is a single thread-safe connection Manager with only one connection at a time. Httpclientconnectionmanager connmrg=new Basichttpclientconnectionmanager (); Httproute route=new httproute ("localhost", 80);//Request route//Please new connection, this may be a long process connectionrequest connrequest= Connmrg.requestconnection (route, NULL);//wait to connect for 10 seconds httpclientconnection Conn=connrequest.get (timeunit.seconds); /If necessary, use Connectionrequest#cancel () to terminate the connection request prematurely and will unlock the blocking thread of Connectionrequest#get () try {//If the connection does not open if (!conn.isopen ()) {// Based on its routing information, a connection is created within 1000 milliseconds (conn, route, Connmrg.connect, context);/////When the route is complete, i.e. all intermediate hops are completed, the connection is fully established at the time Mark Connmrg.routecomplete ( Conn, route, context);} Do something with a connection ... finally {//releases the connection to return to the manager, the connection survives for 1 minutes, making it easy for other consumers to use CONNMRG.RELEASECONNECTION (conn, NULL, 1, timeunit.minutes);}} /** * poolinghttpclientconnectionmanager-manages client connection pooling and can serve connection requests from multiple execution processes. * These connections are pooled on a per-route basis. The manager leases a persistent available connection to a route for a request instead of creating a new connection. * The implementation of connection management defaults to creating a concurrent connection of no more than 2 and a total of no more than 20 for each route. * However, in real-world applications these limitationsLimited performance increase, especially when HTTP protocol transfer service. * However, the following example shows how to adjust the parameters of a connection pool: */public void m2 () {Poolinghttpclientconnectionmanager cm=new Poolinghttpclientconnectionmanager ();//Set the default maximum number of concurrent connections for a single route to 20cm.setdefaultmaxperroute (20);//For address localhost : 80 of Host settings maximum concurrent connections is 50HttpHost localhost=new httphost ("localhost"), Cm.setmaxperroute (new Httproute (localhost), 50); Closeablehttpclient Client=httpclients.custom (). Setconnectionmanager (CM). Build ();/* Connection Manager shutdown: Ensure that connections in the manager are closed, And the system resources allocated by these connections are freed *///closablehttpclient httpClient = <...>//httpclient.close ();} /** * Perform multithreaded requests---httpclient is thread-safe and shareable when executing multithreaded requests. * It is recommended that each thread should have a dedicated HttpContext instance. * @throws interruptedexception */public void M3 () throws Interruptedexception{poolinghttpclientconnectionmanager cm= New Poolinghttpclientconnectionmanager (); Closeablehttpclient Client=httpclients.custom (). Setconnectionmanager (CM). Build ();//uris to perform GETs onstring[] uristoget={"http://www.domain1.com", "http://www.domain2.com", "http://www.domain3.com", "http://www.domain4.com" };//create a thread foR each urigetthread[] threads=new getthread[uristoget.length];for (int i = 0; i < threads.length; i++) {HttpGet HttpGet =new HttpGet (Uristoget[i]); threads[i]=new getthread (client, httpget);} Start the threadsfor (int i = 0; i < threads.length; i++) {Threads[i].start ();} Join the threadsfor (int i = 0; i < threads.length; i++) {Threads[i].join ();}} /** * Connection remains alive policy with keep-alive header */public void M4 () {Connectionkeepalivestrategy mystrategy=new connectionkeepalivestrategy ( {Public long getkeepaliveduration (httpresponse response, HttpContext context) {//honor ' keep-alive ' Headerheaderelementiterator iterator=new Basicheaderelementiterator (Response.headeriterator (HTTP. conn_keep_alive)); while (Iterator.hasnext ()) {HeaderElement he = (headerelement) iterator.next (); String Param=he.getname (); String Value=he.getvalue (), if (value! = null && param.equalsignorecase ("timeout")) {try {return Long.parselong ( Value) *1000;} catch (NumberFormatException e) {}}}httphost target = (httphost) ContexT.getattribute (Httpclientcontext.http_target_host); if ("www.naughty-server.com". Equalsignorecase ( Target.gethostname ())) {//keep alive for 5 Seconds Onlyreturn 5*1000;} Else{//otherwise keep alive for Secondsreturn 30*1000;}}; Closeablehttpclient Client=httpclients.custom (). Setkeepalivestrategy (Mystrategy). build (); /** * Connection Socket factory-relies on the Connectionsocketfactory interface to create, initialize, and connect sockets. * Plainconnectionsocketfactory is the default factory class for creating and initializing native (non-encrypted) sockets. * * Secure Sockets Layer Connection factory class Layeredconnectionsocketfactory is an extension of the Connectionsocketfactory interface, which allows you to create sockets layer on a pure socket. * * HttpClient can also be used to implement the sslsocketfactory of the SSL/TLS layer. Note HttpClient does not work with any custom encryption features. * It relies entirely on standard Java Cryptography (JCE) and Secure Sockets Extensions (Jsee). * @throws ioexception */public void M5 () throws Ioexception{httpclientcontext Clientcontext=httpclientcontext.create () ; Plainconnectionsocketfactory sf=plainconnectionsocketfactory.getsocketfactory ();//creating and initializing Socket sockets socket= Sf.createsocket (clientcontext); int timeout=1000;//mshttphost target=new httphost ("localhost"); inetsocketaddress RemoteaddrEss=new inetsocketaddress (inetaddress.getbyaddress (New byte[]{127,0,0,1}), 80);//Connection Socket Sf.connectsocket (timeout, Socket, target, remoteaddress, NULL, clientcontext);} /** * Connection Socket factory Consolidated Connection Manager: * Custom Connection The socket factory can be associated with a special protocol such as HTTP or HTTPS and then used to create a custom connection Manager */public void M6 () { Connectionsocketfactory plainsf=plainconnectionsocketfactory.getsocketfactory (); Layeredconnectionsocketfactory sslsf=sslconnectionsocketfactory.getsocketfactory (); registry<connectionsocketfactory> r = registrybuilder.<connectionsocketfactory>create (). Register (" http ", PLAINSF). Register (" https ", SSLSF). Build (); Httpclientconnectionmanager cm=new Poolinghttpclientconnectionmanager (R); Closeablehttpclient Client=httpclients.custom (). Setconnectionmanager (CM). Build ();} /** * SSL/TLS customization: * httpclient use Sslconnectionsocketfactory to create an SSL connection. Sslconnectionsocketfactory allows for highly customizable. * It uses an instance of Javax.net.ssl.SSLContext as an argument and uses it to create a custom-configured SSL connection. * This section requires a certain degree of understanding of the SSL/TLS protocol. */public void M7 () {//keystore Mytruststore = <...>//sslcontext SslconteXT = Sslcontexts.custom (). Loadtrustmaterial (Mytruststore). build ();//sslconnectionsocketfactory sslsf=new Sslconnectionsocketfactory (Sslcontext);} /** * Hostname Verification: In addition to playing trust authentication and client authentication roles at the SSL/TSL protocol layer, httpclient can selectively verify that the target host name is the same as the name stored on the server in the certificate. Once the connection has been established, the verification process provides additional reliability assurance for the server * Trust material. * The Javax.net.ssl.HostnameVerifier interface represents a host name validation policy. * There are two implementations of Javax.net.ssl.HostnameVerifier HttpClient can be used to work. * Note: Host name verification and SSL trust verification are not confusing. * * (1) Defaulthostnameverifier: Default implementation, follow RFC2818. The hostname must match any alternate name on the specified certificate. * (2) Noophostnameverifier: This hostname validator essentially turns off host name validation. It accepts any valid and compliant SSL sessions with the target host. */public void M8 () {/* Each httpclient will use the default Defaulthostnameverifier implementation, and if you wish you can also specify implementation */sslcontext sslcontext= Sslcontexts.createsystemdefault ();//sslconnectionsocketfactory SSLSF = new Sslconnectionsocketfactory (SslContext, noophostnameverifier.instance);//httpclient version 4.4 uses a public suffix list to ensure that wildcard characters in SSL certificates are not misused when applied to subdomains that have a common top-level domain name. "Reference: Https://publicsuffix.org/list/effective_tld_names.dat". It is highly recommended to make a local copy of the list, from the source location//location that is https://publicsuffix.org/list/effective_tld_names.dat download this listNo more than once a day. Publicsuffixmatcher Publicsuffixmatcher = publicsuffixmatcherloader.load (// PublicSuffixMatcher.class.getResource ("My-copy-effective_tld_names.dat"));//defaulthostnameverifier Hostnameverifier = new Defaulthostnameverifier (publicsuffixmatcher);//disable public suffix validation//defaulthostnameverifier Hostnameverifier = new Defaulthostnameverifier (null);} /** * HttpClient proxy configuration: Connect to the target host via proxy. * Even though HttpClient is aware of complex routing schemes and proxy chains, it only supports simple, straightforward, or out-of-box one-hop proxy connections. * There are three configurations in the example */public void M9 () {//1. Default proxy parameters//httphost proxy = new Httphost ("Someproxy", 8080);// Defaultproxyrouteplanner routeplanner=new Defaultproxyrouteplanner (proxy);//or 2. Obtain proxy information with the standard JRE proxy selector// Systemdefaultrouteplanner routeplanner=new Systemdefaultrouteplanner (//proxyselector.getdefault ());//or 3. Custom implementation Routeplanner to fully control the process of HTTP route calculation Httprouteplanner Routeplanner = new Httprouteplanner () {public Httproute Determineroute (httphost target, HttpRequest request, HttpContext context) throws HttpException {return new Httproute ( target,//Destination host null,//Local address new Httphost ("SomeprOxy ", 8080),//Agent Host" https ". Equalsignorecase (Target.getschemename ()));//Is safe}}; Closeablehttpclient Client=httpclients.custom (). Setrouteplanner (Routeplanner). build ();}}
</pre ><pre code_snippet_id= "1625884" snippet_file_name= "blog_20160327_9_8684436" name= "code" class= "Java" >
</pre><pre code_snippet_id= "1625884" snippet_file_name= "blog_20160327_5_7004806" name= "code" class= "Java" ><pre name= "code" class= "java" >package com.cycore.dbs.common.demo.hc;import java.util.concurrent.TimeUnit ; Import org.apache.http.conn.httpclientconnectionmanager;/** * httpclient Connection Recycling policy: * Classic blocking i/ One of the main drawbacks of the O model is that network sockets can react to I/O events only when they are blocked during IO operations. * When a connection is released back to the manager, it can remain active but cannot listen for socket status and respond to I/O events. * If the server connection is turned off, the client connection cannot detect a change in the connection status (and cannot respond reasonably to the client by closing the socket). * * HttpClient attempts to mitigate this problem by testing whether the connection is ' stale ', the connection is no longer valid because it was shut down by the server before executing an HTTP request. * The only viable solution that does not introduce a thread to each socket model for an idle connection is a dedicated listener thread that recycles connections that are considered to be inactive for long periods of time. * Listener Threads can call Clientconnectionmanager#closeexpiredconnections () to close expired connections and reclaim closed connections, or call * clientconnectionmanager# Closeidleconnections () to close all idle connections over a period of time. * @author XIAWQ * */public class Idleconnectionmonitorthread extends thread{private final Httpclientconnectionmanager con Nmgr;private volatile Boolean shutdown;public idleconnectionmonitorthread (Httpclientconnectionmanager connMgr) { Super (); this.connmgr = CoNnmgr;} @Overridepublic void Run () {try {while (!shutdown) {synchronized (this) {wait (//close); expired Connectionsconnmgr.closeexpiredconnections ();//optionally, close connections that has been idle longer than 30 Sec.connMgr.closeIdleConnections (Timeunit.seconds);}} catch (Interruptedexception e) {e.printstacktrace ();}} public void shutdown () {shutdown=true;synchronized (this) {notifyall ();//Wake Up Other Threads}}}
</pre><pre code_snippet_id= "1625884" snippet_file_name= "blog_20160327_9_8684436" name= "code" class= "Java" >
<pre name= "code" class= "java" >package com.cycore.dbs.common.demo.hc;import Java.io.file;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.util.date;import org.apache.http.HttpEntity; Import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.methods.closeablehttpresponse;import Org.apache.http.client.methods.httpget;import Org.apache.http.client.protocol.httpclientcontext;import Org.apache.http.impl.client.closeablehttpclient;import Org.apache.http.protocol.httpcontext;public class GetThread extends Thread {private final closeablehttpclient Httpclient;private final HttpContext context;private final HttpGet httpget;public GetThread (closeablehttpclient HttpClient, HttpGet httpget) {this.httpclient = Httpclient;this.context = Httpclientcontext.create (); This.httpGet = HttpGet;} @Overridepublic void Run () {try {closeablehttpresponse Response=httpclient.execute (httpget, context); try {httpentity entity = response.getentity (); Entity.writeto (new FileOutputStream (New File ("d:\\" +new Date (). GetTime () + ". txt"))); finally {response.close ();}} catch (Clientprotocolexception e) {//handle protocal errors} catch (IOException e) {//handle I/O Errors}}}
HttpClient 4.5.1---connection management