@ -2,7 +2,7 @@
* HTTPClient
* Copyright 2010 by Sebastian Gaebel
* First released 01.07 .2010 at http : //yacy.net
*
*
* $LastChangedDate$
* $LastChangedRevision$
* $LastChangedBy$
@ -11,12 +11,12 @@
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21 . txt
* If not , see < http : //www.gnu.org/licenses/>.
@ -35,8 +35,8 @@ import java.security.cert.CertificateException;
import java.security.cert.X509Certificate ;
import java.util.LinkedHashMap ;
import java.util.Map ;
import java.util.Set ;
import java.util.Map.Entry ;
import java.util.Set ;
import java.util.concurrent.TimeUnit ;
import javax.net.ssl.SSLContext ;
@ -55,12 +55,16 @@ import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHost ;
import org.apache.http.HttpResponse ;
import org.apache.http.HttpVersion ;
import org.apache.http.auth.AuthScope ;
import org.apache.http.auth.UsernamePasswordCredentials ;
import org.apache.http.client.CredentialsProvider ;
import org.apache.http.client.HttpClient ;
import org.apache.http.client.methods.HttpGet ;
import org.apache.http.client.methods.HttpHead ;
import org.apache.http.client.methods.HttpPost ;
import org.apache.http.client.methods.HttpUriRequest ;
import org.apache.http.client.params.HttpClientParams ;
import org.apache.http.client.protocol.ClientContext ;
import org.apache.http.conn.ClientConnectionManager ;
import org.apache.http.conn.params.ConnRouteParams ;
import org.apache.http.conn.routing.HttpRoute ;
@ -73,6 +77,7 @@ import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody ;
import org.apache.http.entity.mime.content.StringBody ;
import org.apache.http.impl.client.AbstractHttpClient ;
import org.apache.http.impl.client.BasicCredentialsProvider ;
import org.apache.http.impl.client.DefaultHttpClient ;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager ;
import org.apache.http.params.BasicHttpParams ;
@ -87,7 +92,7 @@ import org.apache.http.util.EntityUtils;
/ * *
* HttpClient implementation which uses HttpComponents Client { @link http : //hc.apache.org/}
*
*
* @author sixcooler
*
* /
@ -96,6 +101,7 @@ public class HTTPClient {
private final static int maxcon = 200 ;
private static IdledConnectionEvictor idledConnectionEvictor = null ;
private static HttpClient httpClient = initConnectionManager ( ) ;
private static final CredentialsProvider credsProvider = new BasicCredentialsProvider ( ) ;
private Set < Entry < String , String > > headers = null ;
private HttpResponse httpResponse = null ;
private HttpUriRequest currentRequest = null ;
@ -105,33 +111,33 @@ public class HTTPClient {
private String host = null ;
private boolean redirecting = true ;
private String realm = null ;
public HTTPClient ( ) {
super ( ) ;
}
public HTTPClient ( final String userAgent ) {
super ( ) ;
this . userAgent = userAgent ;
}
public HTTPClient ( final String userAgent , final int timeout ) {
super ( ) ;
this . userAgent = userAgent ;
this . timeout = timeout ;
}
public static void setDefaultUserAgent ( final String defaultAgent ) {
HttpProtocolParams . setUserAgent ( httpClient . getParams ( ) , defaultAgent ) ;
}
public static HttpClient initConnectionManager ( ) {
// Create and initialize scheme registry
final SchemeRegistry schemeRegistry = new SchemeRegistry ( ) ;
schemeRegistry . register ( new Scheme ( "http" , 80 , PlainSocketFactory . getSocketFactory ( ) ) ) ;
schemeRegistry . register ( new Scheme ( "https" , 443 , getSSLSocketFactory ( ) ) ) ;
ThreadSafeClientConnManager clientConnectionManager = new ThreadSafeClientConnManager ( schemeRegistry ) ;
final ThreadSafeClientConnManager clientConnectionManager = new ThreadSafeClientConnManager ( schemeRegistry ) ;
// Create and initialize HTTP parameters
final HttpParams httpParams = new BasicHttpParams ( ) ;
@ -145,7 +151,7 @@ public class HTTPClient {
// connections per host (2 default)
clientConnectionManager . setDefaultMaxPerRoute ( 2 ) ;
// Increase max connections for localhost
HttpHost localhost = new HttpHost ( "localhost" ) ;
final HttpHost localhost = new HttpHost ( "localhost" ) ;
clientConnectionManager . setMaxForRoute ( new HttpRoute ( localhost ) , maxcon ) ;
/ * *
* HTTP protocol settings
@ -170,7 +176,7 @@ public class HTTPClient {
HttpConnectionParams . setTcpNoDelay ( httpParams , false ) ;
// Defines whether the socket can be bound even though a previous connection is still in a timeout state.
HttpConnectionParams . setSoReuseaddr ( httpParams , true ) ;
httpClient = new DefaultHttpClient ( clientConnectionManager , httpParams ) ;
// ask for gzip
( ( AbstractHttpClient ) httpClient ) . addRequestInterceptor ( new GzipRequestInterceptor ( ) ) ;
@ -183,12 +189,12 @@ public class HTTPClient {
}
return httpClient ;
}
/ * *
* This method should be called just before shutdown
* to stop the ConnectionManager and idledConnectionEvictor
*
* @throws InterruptedException
*
* @throws InterruptedException
* /
public static void closeConnectionManager ( ) throws InterruptedException {
if ( idledConnectionEvictor ! = null ) {
@ -200,79 +206,86 @@ public class HTTPClient {
// Shut down the connection manager
httpClient . getConnectionManager ( ) . shutdown ( ) ;
}
}
public static void setAuth ( final String host , final int port , final String user , final String pw ) {
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials ( user , pw ) ;
final AuthScope scope = new AuthScope ( host , port ) ;
credsProvider . setCredentials ( scope , creds ) ;
httpClient . getParams ( ) . setParameter ( ClientContext . CREDS_PROVIDER , credsProvider ) ;
}
/ * *
* this method sets a host on which more than the default of 2 router per host are allowed
*
*
* @param the host to be raised in ' route per host '
* /
public static void setMaxRouteHost ( final String host ) {
HttpHost mHost = new HttpHost ( host ) ;
final HttpHost mHost = new HttpHost ( host ) ;
( ( ThreadSafeClientConnManager ) httpClient . getConnectionManager ( ) ) . setMaxForRoute ( new HttpRoute ( mHost ) , 50 ) ;
}
/ * *
* This method sets the Header used for the request
*
*
* @param entrys to be set as request header
* /
public void setHeader ( final Set < Entry < String , String > > entrys ) {
this . headers = entrys ;
}
/ * *
* This method sets the timeout of the Connection and Socket
*
*
* @param timeout in milliseconds
* /
public void setTimout ( final int timeout ) {
this . timeout = timeout ;
}
/ * *
* This method sets the UserAgent to be used for the request
*
*
* @param userAgent
* /
public void setUserAgent ( final String userAgent ) {
this . userAgent = userAgent ;
}
/ * *
* This method sets the host to be called at the request
*
*
* @param host
* /
public void setHost ( final String host ) {
this . host = host ;
}
/ * *
* This method sets if requests should follow redirects
*
*
* @param redirecting
* /
public void setRedirecting ( final boolean redirecting ) {
this . redirecting = redirecting ;
}
/ * *
* This method sets the authorization realm for the request
*
*
* @param realm
* /
public void setRealm ( final String realm ) {
this . realm = realm ;
}
/ * *
* This method GETs a page from the server .
*
*
* @param uri the url to get
* @return content bytes
* @throws IOException
* @throws IOException
* /
public byte [ ] GETbytes ( final String uri ) throws IOException {
return GETbytes ( uri , Long . MAX_VALUE ) ;
@ -280,64 +293,64 @@ public class HTTPClient {
/ * *
* This method GETs a page from the server .
*
*
* @param uri the url to get
* @return content bytes
* @throws IOException
* @throws IOException
* /
public byte [ ] GETbytes ( final MultiProtocolURI url ) throws IOException {
return GETbytes ( url , Long . MAX_VALUE ) ;
}
/ * *
* This method GETs a page from the server .
*
*
* @param uri the url to get
* @param maxBytes to get
* @return content bytes
* @throws IOException
* @throws IOException
* /
public byte [ ] GETbytes ( final String uri , long maxBytes ) throws IOException {
public byte [ ] GETbytes ( final String uri , final long maxBytes ) throws IOException {
return GETbytes ( new MultiProtocolURI ( uri ) , maxBytes ) ;
}
/ * *
* This method GETs a page from the server .
*
*
* @param uri the url to get
* @param maxBytes to get
* @return content bytes
* @throws IOException
* @throws IOException
* /
public byte [ ] GETbytes ( final MultiProtocolURI url , long maxBytes ) throws IOException {
boolean localhost = url . getHost ( ) . equals ( "localhost" ) ;
String urix = url . toNormalform ( true , false , ! localhost , false ) ;
public byte [ ] GETbytes ( final MultiProtocolURI url , final long maxBytes ) throws IOException {
final boolean localhost = url . getHost ( ) . equals ( "localhost" ) ;
final String urix = url . toNormalform ( true , false , ! localhost , false ) ;
final HttpGet httpGet = new HttpGet ( urix ) ;
if ( ! localhost ) setHost ( url . getHost ( ) ) ; // overwrite resolved IP, needed for shared web hosting DO NOT REMOVE, see http://en.wikipedia.org/wiki/Shared_web_hosting_service
return getContentBytes ( httpGet , maxBytes ) ;
}
/ * *
* This method GETs a page from the server .
* to be used for streaming out
* Please take care to call finish ( ) !
*
*
* @param uri the url to get
* @throws IOException
* /
public void GET ( final String uri ) throws IOException {
if ( currentRequest ! = null ) throw new IOException ( "Client is in use!" ) ;
if ( this . currentRequest ! = null ) throw new IOException ( "Client is in use!" ) ;
final MultiProtocolURI url = new MultiProtocolURI ( uri ) ;
final HttpGet httpGet = new HttpGet ( url . toNormalform ( true , false , true , false ) ) ;
setHost ( url . getHost ( ) ) ; // overwrite resolved IP, needed for shared web hosting DO NOT REMOVE, see http://en.wikipedia.org/wiki/Shared_web_hosting_service
currentRequest = httpGet ;
this . currentRequest = httpGet ;
execute ( httpGet ) ;
}
/ * *
* This method gets HEAD response
*
*
* @param uri the url to Response from
* @return the HttpResponse
* @throws IOException
@ -349,21 +362,21 @@ public class HTTPClient {
execute ( httpHead ) ;
finish ( ) ;
ConnectionInfo . removeConnection ( httpHead . hashCode ( ) ) ;
return httpResponse;
return this . httpResponse;
}
/ * *
* This method POSTs a page from the server .
* to be used for streaming out
* Please take care to call finish ( ) !
*
*
* @param uri the url to post
* @param instream the input to post
* @param length the contentlength
* @throws IOException
* @throws IOException
* /
public void POST ( final String uri , final InputStream instream , long length ) throws IOException {
if ( currentRequest ! = null ) throw new IOException ( "Client is in use!" ) ;
public void POST ( final String uri , final InputStream instream , final long length ) throws IOException {
if ( this . currentRequest ! = null ) throw new IOException ( "Client is in use!" ) ;
final MultiProtocolURI url = new MultiProtocolURI ( uri ) ;
final HttpPost httpPost = new HttpPost ( url . toNormalform ( true , false , true , false ) ) ;
String host = url . getHost ( ) ;
@ -371,19 +384,19 @@ public class HTTPClient {
setHost ( host ) ; // overwrite resolved IP, needed for shared web hosting DO NOT REMOVE, see http://en.wikipedia.org/wiki/Shared_web_hosting_service
final NonClosingInputStreamEntity inputStreamEntity = new NonClosingInputStreamEntity ( instream , length ) ;
// statistics
upbytes = length ;
this . upbytes = length ;
httpPost . setEntity ( inputStreamEntity ) ;
currentRequest = httpPost ;
this . currentRequest = httpPost ;
execute ( httpPost ) ;
}
/ * *
* send data to the server named by uri
*
*
* @param uri the url to post
* @param parts to post
* @return content bytes
* @throws IOException
* @throws IOException
* /
public byte [ ] POSTbytes ( final String uri , final Map < String , ContentBody > parts , final boolean usegzip ) throws IOException {
final MultiProtocolURI url = new MultiProtocolURI ( uri ) ;
@ -392,7 +405,7 @@ public class HTTPClient {
/ * *
* send data to the server named by vhost
*
*
* @param url address of the server
* @param vhost name of the server at address which should respond
* @param post data to send ( name - value - pairs )
@ -402,7 +415,7 @@ public class HTTPClient {
* /
public byte [ ] POSTbytes ( final MultiProtocolURI url , final String vhost , final Map < String , ContentBody > post , final boolean usegzip ) throws IOException {
final HttpPost httpPost = new HttpPost ( url . toNormalform ( true , false , true , false ) ) ;
setHost ( vhost ) ; // overwrite resolved IP, needed for shared web hosting DO NOT REMOVE, see http://en.wikipedia.org/wiki/Shared_web_hosting_service
if ( vhost = = null ) setHost ( "127.0.0.1" ) ;
@ -410,27 +423,27 @@ public class HTTPClient {
for ( final Entry < String , ContentBody > part : post . entrySet ( ) )
multipartEntity . addPart ( part . getKey ( ) , part . getValue ( ) ) ;
// statistics
upbytes = multipartEntity . getContentLength ( ) ;
this . upbytes = multipartEntity . getContentLength ( ) ;
if ( usegzip ) {
httpPost . setEntity ( new GzipCompressingEntity ( multipartEntity ) ) ;
} else {
httpPost . setEntity ( multipartEntity ) ;
}
return getContentBytes ( httpPost , Long . MAX_VALUE ) ;
}
/ * *
* send stream - data to the server named by uri
*
*
* @param uri the url to post
* @param instream the stream to send
* @param length the length of the stream
* @return content bytes
* @throws IOException
* /
public byte [ ] POSTbytes ( final String uri , final InputStream instream , long length ) throws IOException {
public byte [ ] POSTbytes ( final String uri , final InputStream instream , final long length ) throws IOException {
final MultiProtocolURI url = new MultiProtocolURI ( uri ) ;
final HttpPost httpPost = new HttpPost ( url . toNormalform ( true , false , true , false ) ) ;
String host = url . getHost ( ) ;
@ -439,27 +452,27 @@ public class HTTPClient {
final InputStreamEntity inputStreamEntity = new InputStreamEntity ( instream , length ) ;
// statistics
upbytes = length ;
this . upbytes = length ;
httpPost . setEntity ( inputStreamEntity ) ;
return getContentBytes ( httpPost , Long . MAX_VALUE ) ;
}
/ * *
*
*
* @return HttpResponse from call
* /
public HttpResponse getHttpResponse ( ) {
return httpResponse;
return this . httpResponse;
}
/ * *
*
*
* @return status code from http request
* /
public int getStatusCode ( ) {
return httpResponse. getStatusLine ( ) . getStatusCode ( ) ;
return this . httpResponse. getStatusLine ( ) . getStatusCode ( ) ;
}
/ * *
* This method gets direct access to the content - stream
* Since this way is uncontrolled by the Client think of using ' writeTo ' instead !
@ -469,20 +482,20 @@ public class HTTPClient {
* @throws IOException
* /
public InputStream getContentstream ( ) throws IOException {
if ( httpResponse ! = null & & currentRequest ! = null ) {
final HttpEntity httpEntity = httpResponse. getEntity ( ) ;
if ( this . httpResponse ! = null & & this . currentRequest ! = null ) {
final HttpEntity httpEntity = this . httpResponse. getEntity ( ) ;
if ( httpEntity ! = null ) try {
return httpEntity . getContent ( ) ;
} catch ( final IOException e ) {
ConnectionInfo . removeConnection ( currentRequest. hashCode ( ) ) ;
currentRequest. abort ( ) ;
currentRequest = null ;
ConnectionInfo . removeConnection ( this . currentRequest. hashCode ( ) ) ;
this . currentRequest. abort ( ) ;
this . currentRequest = null ;
throw e ;
}
}
return null ;
}
/ * *
* This method streams the content to the outputStream
* Please take care to call finish ( ) !
@ -491,24 +504,24 @@ public class HTTPClient {
* @throws IOException
* /
public void writeTo ( final OutputStream outputStream ) throws IOException {
if ( httpResponse ! = null & & currentRequest ! = null ) {
final HttpEntity httpEntity = httpResponse. getEntity ( ) ;
if ( this . httpResponse ! = null & & this . currentRequest ! = null ) {
final HttpEntity httpEntity = this . httpResponse. getEntity ( ) ;
if ( httpEntity ! = null ) try {
httpEntity . writeTo ( outputStream ) ;
outputStream . flush ( ) ;
// Ensures that the entity content is fully consumed and the content stream, if exists, is closed.
EntityUtils . consume ( httpEntity ) ;
ConnectionInfo . removeConnection ( currentRequest. hashCode ( ) ) ;
currentRequest = null ;
ConnectionInfo . removeConnection ( this . currentRequest. hashCode ( ) ) ;
this . currentRequest = null ;
} catch ( final IOException e ) {
ConnectionInfo . removeConnection ( currentRequest. hashCode ( ) ) ;
currentRequest. abort ( ) ;
currentRequest = null ;
ConnectionInfo . removeConnection ( this . currentRequest. hashCode ( ) ) ;
this . currentRequest. abort ( ) ;
this . currentRequest = null ;
throw e ;
}
}
}
/ * *
* This method ensures correct finish of client - connections
* This method should be used after every use of GET or POST and writeTo or getContentstream !
@ -516,35 +529,35 @@ public class HTTPClient {
* @throws IOException
* /
public void finish ( ) throws IOException {
if ( httpResponse ! = null ) {
final HttpEntity httpEntity = httpResponse. getEntity ( ) ;
if ( this . httpResponse ! = null ) {
final HttpEntity httpEntity = this . httpResponse. getEntity ( ) ;
if ( httpEntity ! = null & & httpEntity . isStreaming ( ) ) {
// Ensures that the entity content is fully consumed and the content stream, if exists, is closed.
EntityUtils . consume ( httpEntity ) ;
}
}
if ( currentRequest ! = null ) {
ConnectionInfo . removeConnection ( currentRequest. hashCode ( ) ) ;
currentRequest. abort ( ) ;
currentRequest = null ;
if ( this . currentRequest ! = null ) {
ConnectionInfo . removeConnection ( this . currentRequest. hashCode ( ) ) ;
this . currentRequest. abort ( ) ;
this . currentRequest = null ;
}
}
private byte [ ] getContentBytes ( final HttpUriRequest httpUriRequest , final long maxBytes ) throws IOException {
byte [ ] content = null ;
try {
execute ( httpUriRequest ) ;
if ( httpResponse = = null ) return null ;
if ( this . httpResponse = = null ) return null ;
// get the response body
final HttpEntity httpEntity = httpResponse. getEntity ( ) ;
final HttpEntity httpEntity = this . httpResponse. getEntity ( ) ;
if ( httpEntity ! = null ) {
if ( getStatusCode ( ) = = 200 & & httpEntity . getContentLength ( ) < maxBytes ) {
try {
content = EntityUtils . toByteArray ( httpEntity ) ;
} catch ( OutOfMemoryError e ) {
} catch ( final OutOfMemoryError e ) {
throw new IOException ( e . toString ( ) ) ;
}
}
}
// Ensures that the entity content is fully consumed and the content stream, if exists, is closed.
EntityUtils . consume ( httpEntity ) ;
}
@ -556,7 +569,7 @@ public class HTTPClient {
ConnectionInfo . removeConnection ( httpUriRequest . hashCode ( ) ) ;
return content ;
}
private void execute ( final HttpUriRequest httpUriRequest ) throws IOException {
final HttpContext httpContext = new BasicHttpContext ( ) ;
setHeaders ( httpUriRequest ) ;
@ -566,8 +579,8 @@ public class HTTPClient {
storeConnectionInfo ( httpUriRequest ) ;
// execute the method; some asserts confirm that that the request can be send with Content-Length and is therefore not terminated by EOF
if ( httpUriRequest instanceof HttpEntityEnclosingRequest ) {
HttpEntityEnclosingRequest hrequest = ( HttpEntityEnclosingRequest ) httpUriRequest ;
HttpEntity entity = hrequest . getEntity ( ) ;
final HttpEntityEnclosingRequest hrequest = ( HttpEntityEnclosingRequest ) httpUriRequest ;
final HttpEntity entity = hrequest . getEntity ( ) ;
assert entity ! = null ;
//assert !entity.isChunked();
//assert entity.getContentLength() >= 0;
@ -575,45 +588,45 @@ public class HTTPClient {
}
try {
long time = System . currentTimeMillis ( ) ;
httpResponse = httpClient . execute ( httpUriRequest , httpContext ) ;
httpResponse. setHeader ( HeaderFramework . RESPONSE_TIME_MILLIS , Long . toString ( System . currentTimeMillis ( ) - time ) ) ;
} catch ( IOException e ) {
final long time = System . currentTimeMillis ( ) ;
this . httpResponse = httpClient . execute ( httpUriRequest , httpContext ) ;
this . httpResponse. setHeader ( HeaderFramework . RESPONSE_TIME_MILLIS , Long . toString ( System . currentTimeMillis ( ) - time ) ) ;
} catch ( final IOException e ) {
ConnectionInfo . removeConnection ( httpUriRequest . hashCode ( ) ) ;
httpUriRequest . abort ( ) ;
throw new IOException ( "Client can't execute: " + e . getMessage ( ) ) ;
}
}
private void setHeaders ( final HttpUriRequest httpUriRequest ) {
if ( headers ! = null ) {
for ( final Entry < String , String > entry : headers) {
if ( this . headers ! = null ) {
for ( final Entry < String , String > entry : this . headers) {
httpUriRequest . setHeader ( entry . getKey ( ) , entry . getValue ( ) ) ;
}
}
if ( host ! = null )
httpUriRequest . setHeader ( HTTP . TARGET_HOST , host) ;
if ( realm ! = null )
httpUriRequest . setHeader ( "Authorization" , "realm=" + realm) ;
if ( this . host ! = null )
httpUriRequest . setHeader ( HTTP . TARGET_HOST , this . host) ;
if ( this . realm ! = null )
httpUriRequest . setHeader ( "Authorization" , "realm=" + this . realm) ;
}
private void setParams ( final HttpParams httpParams ) {
HttpClientParams . setRedirecting ( httpParams , redirecting) ;
HttpConnectionParams . setConnectionTimeout ( httpParams , timeout) ;
HttpConnectionParams . setSoTimeout ( httpParams , timeout) ;
if ( userAgent ! = null )
HttpProtocolParams . setUserAgent ( httpParams , userAgent) ;
if ( host ! = null )
httpParams . setParameter ( HTTP . TARGET_HOST , host) ;
}
HttpClientParams . setRedirecting ( httpParams , this . redirecting) ;
HttpConnectionParams . setConnectionTimeout ( httpParams , this . timeout) ;
HttpConnectionParams . setSoTimeout ( httpParams , this . timeout) ;
if ( this . userAgent ! = null )
HttpProtocolParams . setUserAgent ( httpParams , this . userAgent) ;
if ( this . host ! = null )
httpParams . setParameter ( HTTP . TARGET_HOST , this . host) ;
}
private void setProxy ( final HttpParams httpParams ) {
if ( ProxySettings . use )
ConnRouteParams . setDefaultProxy ( httpParams , ProxySettings . getProxyHost ( ) ) ;
// TODO find a better way for this
ProxySettings . setProxyCreds ( ( AbstractHttpClient ) httpClient ) ;
}
private void storeConnectionInfo ( final HttpUriRequest httpUriRequest ) {
final int port = httpUriRequest . getURI ( ) . getPort ( ) ;
final String thost = httpUriRequest . getURI ( ) . getHost ( ) ;
@ -624,16 +637,16 @@ public class HTTPClient {
httpUriRequest . getMethod ( ) + " " + httpUriRequest . getURI ( ) . getPath ( ) ,
httpUriRequest . hashCode ( ) ,
System . currentTimeMillis ( ) ,
upbytes) ) ;
this . upbytes) ) ;
}
private static SSLSocketFactory getSSLSocketFactory ( ) {
final TrustManager trustManager = new X509TrustManager ( ) {
public void checkClientTrusted ( X509Certificate [ ] chain , String authType )
public void checkClientTrusted ( final X509Certificate [ ] chain , final String authType )
throws CertificateException {
}
public void checkServerTrusted ( X509Certificate [ ] chain , String authType )
public void checkServerTrusted ( final X509Certificate [ ] chain , final String authType )
throws CertificateException {
}
@ -645,10 +658,10 @@ public class HTTPClient {
try {
sslContext = SSLContext . getInstance ( "TLS" ) ;
sslContext . init ( null , new TrustManager [ ] { trustManager } , null ) ;
} catch ( NoSuchAlgorithmException e ) {
} catch ( final NoSuchAlgorithmException e ) {
// should not happen
// e.printStackTrace();
} catch ( KeyManagementException e ) {
} catch ( final KeyManagementException e ) {
// should not happen
// e.printStackTrace();
}
@ -669,10 +682,10 @@ public class HTTPClient {
try {
newparts . put ( "foo" , new StringBody ( "FooBar" ) ) ;
newparts . put ( "bar" , new StringBody ( "BarFoo" ) ) ;
} catch ( UnsupportedEncodingException e ) {
} catch ( final UnsupportedEncodingException e ) {
System . out . println ( e . getStackTrace ( ) ) ;
}
HTTPClient client = new HTTPClient ( ) ;
final HTTPClient client = new HTTPClient ( ) ;
client . setUserAgent ( "foobar" ) ;
client . setRedirecting ( false ) ;
// Get some
@ -683,7 +696,7 @@ public class HTTPClient {
}
try {
System . out . println ( UTF8 . String ( client . GETbytes ( url ) ) ) ;
} catch ( IOException e ) {
} catch ( final IOException e ) {
e . printStackTrace ( ) ;
}
}
@ -710,14 +723,14 @@ public class HTTPClient {
// Close out connection manager
try {
HTTPClient . closeConnectionManager ( ) ;
} catch ( InterruptedException e ) {
} catch ( final InterruptedException e ) {
e . printStackTrace ( ) ;
}
}
/ * *
*
*
* @see : http : //hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/connmgmt.html#d4e638
*
* /
@ -727,7 +740,7 @@ public class HTTPClient {
private volatile boolean shutdown ;
public IdledConnectionEvictor ( ClientConnectionManager clientConnectionManager ) {
public IdledConnectionEvictor ( final ClientConnectionManager clientConnectionManager ) {
super ( ) ;
this . clientConnectionManager = clientConnectionManager ;
}
@ -735,29 +748,29 @@ public class HTTPClient {
@Override
public void run ( ) {
try {
while ( ! shutdown) {
while ( ! this . shutdown) {
synchronized ( this ) {
wait ( 5000 ) ;
// Close expired connections
clientConnectionManager. closeExpiredConnections ( ) ;
this . clientConnectionManager. closeExpiredConnections ( ) ;
// Optionally, close connections
// that have been idle longer than 5 sec
// (some SOHO router act strange on >5sec idled connections)
clientConnectionManager. closeIdleConnections ( 5 , TimeUnit . SECONDS ) ;
this . clientConnectionManager. closeIdleConnections ( 5 , TimeUnit . SECONDS ) ;
}
}
} catch ( InterruptedException ex ) {
} catch ( final InterruptedException ex ) {
// terminate
}
}
public void shutdown ( ) {
shutdown = true ;
this . shutdown = true ;
synchronized ( this ) {
notifyAll ( ) ;
}
}
}
}