public static HttpClientBuilder getHttpClientBuilder() { // Common CacheConfig for both the JarCacheStorage and the underlying // BasicHttpCacheStorage final CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000).setMaxObjectSize(1024 * 128) .build(); RequestConfig config = RequestConfig.custom().setConnectTimeout(DEFAULT_TIMEOUT) .setConnectionRequestTimeout(DEFAULT_TIMEOUT).setSocketTimeout(DEFAULT_TIMEOUT).build(); HttpClientBuilder clientBuilder = CachingHttpClientBuilder.create() // allow caching .setCacheConfig(cacheConfig) // Wrap the local JarCacheStorage around a BasicHttpCacheStorage .setHttpCacheStorage(new JarCacheStorage(null, cacheConfig, new BasicHttpCacheStorage(cacheConfig))) // Support compressed data // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html#d5e1238 .addInterceptorFirst(new RequestAcceptEncoding()).addInterceptorFirst(new ResponseContentEncoding()) // use system defaults for proxy etc. .useSystemProperties().setDefaultRequestConfig(config); return clientBuilder; }
/** * {@inheritDoc} */ @Override protected BasicHttpProcessor createHttpProcessor() { BasicHttpProcessor result = super.createHttpProcessor(); result.addRequestInterceptor(new RequestAcceptEncoding()); result.addResponseInterceptor(new ResponseContentEncoding()); return result; }
/** * {@inheritDoc} */ @Override protected BasicHttpProcessor createHttpProcessor() { final BasicHttpProcessor result = super.createHttpProcessor(); result.addRequestInterceptor(new RequestAcceptEncoding()); result.addResponseInterceptor(new ResponseContentEncoding()); return result; }
/** * Creates an instance of <tt>DefaultHttpClient</tt> with a <tt>ThreadSafeClientConnManager</tt>. * @return an implementation of the <tt>HttpClient</tt> interface. */ public HttpClient createInstance() { LOGGER.debug("Creating a new instance of DefaultHttpClient with configuration -> {}", toString()); // Create the connection manager which will be default create the necessary schema registry stuff... final PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(); connectionManager.setMaxTotal(maxTotalConnections); connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute); // Set the HTTP connection parameters (These are in the HttpCore JavaDocs, NOT the HttpClient ones)... final HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, connectionTimeout); HttpConnectionParams.setLinger(params, linger); HttpConnectionParams.setSocketBufferSize(params, socketBufferSize); HttpConnectionParams.setSoKeepalive(params, soKeepAlive); HttpConnectionParams.setSoReuseaddr(params, soReuseAddr); HttpConnectionParams.setSoTimeout(params, soTimeout); HttpConnectionParams.setStaleCheckingEnabled(params, staleCheckingEnabled); HttpConnectionParams.setTcpNoDelay(params, tcpNoDelay); // Create the HttpClient and configure the compression interceptors if required... final DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, params); if (useCompression) { httpClient.addRequestInterceptor(new RequestAcceptEncoding()); httpClient.addResponseInterceptor(new DeflateContentEncoding()); } return httpClient; }
private void prepareHttpClient(AbstractHttpClient client) { client.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, true); client.addRequestInterceptor(new RequestAcceptEncoding()); client.addResponseInterceptor(new ResponseContentEncoding()); client.setRedirectStrategy(new BasicRedirectStrategy()); }
@Override public void connect() throws IOException { if (m_client != null) { return; } final HttpParams httpParams = new BasicHttpParams(); if (m_request != null) { int timeout = m_request.getParameterAsInt("timeout"); if (timeout > 0) { HttpConnectionParams.setConnectionTimeout(httpParams, timeout); HttpConnectionParams.setSoTimeout(httpParams, timeout); } } m_client = new DefaultHttpClient(httpParams); m_client.addRequestInterceptor(new RequestAcceptEncoding()); m_client.addResponseInterceptor(new ResponseContentEncoding()); if (m_request != null) { int retries = m_request.getParameterAsInt("retries"); if (retries > 0) { m_client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount <= getRetryCount() && (exception instanceof SocketTimeoutException || exception instanceof ConnectTimeoutException)) { return true; } return super.retryRequest(exception, executionCount, context); } }); } String disableSslVerification = m_request.getParameter("disable-ssl-verification"); if (Boolean.parseBoolean(disableSslVerification)) { final SchemeRegistry registry = m_client.getConnectionManager().getSchemeRegistry(); final Scheme https = registry.getScheme("https"); try { SSLSocketFactory factory = new SSLSocketFactory(SSLContext.getInstance(EmptyKeyRelaxedTrustSSLContext.ALGORITHM), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); final Scheme lenient = new Scheme(https.getName(), https.getDefaultPort(), factory); registry.register(lenient); } catch (NoSuchAlgorithmException e) { log().warn(e.getMessage()); } } } }
/** * Constructs a decorator to ask for and handle compressed * entities on the fly. * @param backend the {@link HttpClient} to use for actually * issuing requests */ public DecompressingHttpClient(HttpClient backend) { this(backend, new RequestAcceptEncoding(), new ResponseContentEncoding()); }
/** * Constructs a decorator to ask for and handle compressed * entities on the fly. * @param backend the {@link HttpClient} to use for actually * issuing requests */ public DecompressingHttpClient(final HttpClient backend) { this(backend, new RequestAcceptEncoding(), new ResponseContentEncoding()); }