@Test public void testHttpRequestGet() throws Exception { RequestConfig.Builder req = RequestConfig.custom(); req.setConnectTimeout(5000); req.setConnectionRequestTimeout(5000); req.setRedirectsEnabled(false); req.setSocketTimeout(5000); req.setExpectContinueEnabled(false); HttpGet get = new HttpGet("http://127.0.0.1:54322/login"); get.setConfig(req.build()); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setDefaultMaxPerRoute(5); HttpClientBuilder builder = HttpClients.custom(); builder.disableAutomaticRetries(); builder.disableRedirectHandling(); builder.setConnectionTimeToLive(5, TimeUnit.SECONDS); builder.setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE); builder.setConnectionManager(cm); CloseableHttpClient client = builder.build(); String s = client.execute(get, new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { assertEquals(301, response.getStatusLine().getStatusCode()); return "success"; } }); assertEquals("success", s); }
@Override public long getKeepAliveDuration( HttpResponse response, HttpContext context) { // If there's a Keep-Alive timeout directive in the response and it's // shorter than our configured max, honor that. Otherwise go with the // configured maximum. long duration = DefaultConnectionKeepAliveStrategy.INSTANCE .getKeepAliveDuration(response, context); if (0 < duration && duration < maxIdleTime) { return duration; } return maxIdleTime; }
/** * Creates asynchronous Apache HTTP client. * * @param settings * settings to use to create client. * @param conf * configuration related to async connection. * @return Instance of {@link CloseableHttpAsyncClient}. */ private CloseableHttpAsyncClient createClient(HttpSettings settings, ApacheHttpClientConfiguration conf) { IOReactorConfig ioReactor = IOReactorConfig.custom().setIoThreadCount(conf.getMaxThreadCount()).build(); HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom() .useSystemProperties() // allow POST redirects .setRedirectStrategy(new LaxRedirectStrategy()).setMaxConnTotal(conf.getMaxTotalConnectionCount()).setMaxConnPerRoute(conf.getMaxRouteConnectionCount()).setDefaultIOReactorConfig(ioReactor) .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()).setDefaultRequestConfig(createDefaultRequestConfig(settings)); if (settings.getProxyUrl() != null) { DefaultProxyRoutePlanner routePlanner = createProxyRoutePlanner(settings, httpClientBuilder); httpClientBuilder.setRoutePlanner(routePlanner); } CloseableHttpAsyncClient httpClient = httpClientBuilder.build(); httpClient.start(); return httpClient; }
/** * 初始化httpclient对象 */ private static void buildHttpClient() { RequestConfig globalConfig = RequestConfig.custom().setConnectTimeout(5000) .setSocketTimeout(5000).build(); CloseableHttpClient httpclient = HttpClients.custom().setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()) .setDefaultRequestConfig(globalConfig).build(); HttpClientUtil.httpclient = httpclient; }
/** * springboot初始化restTemplate */ @Bean public RestTemplate initRestTemplate() { // 长连接保持30秒 PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.SECONDS); // 总连接数 pollingConnectionManager.setMaxTotal(1000); // 同路由的并发数 pollingConnectionManager.setDefaultMaxPerRoute(1000); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setConnectionManager(pollingConnectionManager); // 重试次数,默认是3次,没有开启 httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(2, true)); // 保持长连接配置,需要在头添加Keep-Alive httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()); RequestConfig.Builder builder = RequestConfig.custom(); builder.setConnectionRequestTimeout(200); builder.setConnectTimeout(5000); builder.setSocketTimeout(5000); RequestConfig requestConfig = builder.build(); httpClientBuilder.setDefaultRequestConfig(requestConfig); List<Header> headers = new ArrayList<>(); headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36")); headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate")); headers.add(new BasicHeader("Accept-Language", "zh-CN")); headers.add(new BasicHeader("Connection", "Keep-Alive")); httpClientBuilder.setDefaultHeaders(headers); HttpClient httpClient = httpClientBuilder.build(); // httpClient连接配置,底层是配置RequestConfig HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); // 连接超时 clientHttpRequestFactory.setConnectTimeout(5000); // 数据读取超时时间,即SocketTimeout clientHttpRequestFactory.setReadTimeout(5000); // 连接不够用的等待时间,不宜过长,必须设置,比如连接不够用时,时间过长将是灾难性的 clientHttpRequestFactory.setConnectionRequestTimeout(200); // 缓冲请求数据,默认值是true。通过POST或者PUT大量发送数据时,建议将此属性更改为false,以免耗尽内存。 clientHttpRequestFactory.setBufferRequestBody(false); // 添加内容转换器 List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(); messageConverters.add(initStringHttpMessageConverter()); messageConverters.add(new FormHttpMessageConverter()); messageConverters.add(new MappingJackson2XmlHttpMessageConverter()); messageConverters.add(new MappingJackson2HttpMessageConverter()); RestTemplate restTemplate = new RestTemplate(messageConverters); restTemplate.setRequestFactory(clientHttpRequestFactory); restTemplate.setErrorHandler(new DefaultResponseErrorHandler()); return restTemplate; }
private HttpAsyncClientBuilder initialize() { try { final PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager( new DefaultConnectingIOReactor( IOReactorConfig.custom() .setConnectTimeout( connectTimeout ) .setSoTimeout( readTimeout ) .build() ), RegistryBuilder.<SchemeIOSessionStrategy>create() .register( "http", NoopIOSessionStrategy.INSTANCE ) .register( "https", new SSLIOSessionStrategy( certificateLocation != null ? createSSLContext( certificateLocation, certificatePassword ) : SSLContexts.createDefault(), split( System.getProperty( "https.protocols" ) ), split( System.getProperty( "https.cipherSuites" ) ), new DefaultHostnameVerifier( PublicSuffixMatcherLoader.getDefault() ) ) ) .build() ); connManager.setMaxTotal( maxConnTotal ); connManager.setDefaultMaxPerRoute( maxConnPerRoute ); return ( certificateLocation != null ? HttpAsyncClients.custom() .setSSLContext( createSSLContext( certificateLocation, certificatePassword ) ) : HttpAsyncClients.custom() ) .setMaxConnPerRoute( maxConnPerRoute ) .setConnectionManager( connManager ) .setMaxConnTotal( maxConnTotal ) .setKeepAliveStrategy( DefaultConnectionKeepAliveStrategy.INSTANCE ) .setDefaultRequestConfig( RequestConfig .custom() .setRedirectsEnabled( redirectsEnabled ) .setCookieSpec( cookieSpec ) .build() ) .setDefaultCookieStore( basicCookieStore ); } catch( IOReactorException e ) { throw new UncheckedIOException( e ); } }
public CloseableHttpClient build() throws Exception { HttpClientBuilder builder = HttpClients.custom(); builder.useSystemProperties(); builder .setDefaultSocketConfig(SocketConfig.custom() .setTcpNoDelay(true) .setSoKeepAlive(true) .build() ) .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE); HostnameVerifier hostnameVerifier = sslVerificationMode.verifier(); TrustStrategy trustStrategy = sslVerificationMode.trustStrategy(); KeyStore trustStore = agentTruststore(); SSLContextBuilder sslContextBuilder = SSLContextBuilder.create() .useProtocol(systemEnvironment.get(SystemEnvironment.GO_SSL_TRANSPORT_PROTOCOL_TO_BE_USED_BY_AGENT)); if (trustStore != null || trustStrategy != null) { sslContextBuilder.loadTrustMaterial(trustStore, trustStrategy); } sslContextBuilder.loadKeyMaterial(agentKeystore(), keystorePassword().toCharArray()); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(), hostnameVerifier); builder.setSSLSocketFactory(sslConnectionSocketFactory); return builder.build(); }
/** * Get a new CloseableHttpClient * * @return CloseableHttpClient */ public static CloseableHttpClient newHttpClient() { final SocketConfig socketConfig = SocketConfig.custom() .setSoKeepAlive(Boolean.TRUE).setTcpNoDelay(Boolean.TRUE) .setSoTimeout(SOCKET_TIMEOUT_MS).build(); final PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(); manager.setDefaultMaxPerRoute(MAX_HOSTS); manager.setMaxTotal(MAX_HOSTS); manager.setDefaultSocketConfig(socketConfig); final RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(CONNECTION_TIMEOUT_MS) .setCookieSpec(CookieSpecs.IGNORE_COOKIES) .setStaleConnectionCheckEnabled(Boolean.FALSE) .setSocketTimeout(SOCKET_TIMEOUT_MS).build(); final CloseableHttpClient client = HttpClients .custom() .disableRedirectHandling() .setConnectionManager(manager) .setDefaultRequestConfig(requestConfig) .setConnectionReuseStrategy( new DefaultConnectionReuseStrategy()) .setConnectionBackoffStrategy(new DefaultBackoffStrategy()) .setRetryHandler( new DefaultHttpRequestRetryHandler(MAX_RETRIES, false)) .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()) .build(); return client; }
private HttpClientTemplate() { // 长连接保持30秒 PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager(timeToLive, TimeUnit.SECONDS); // 总连接数 pollingConnectionManager.setMaxTotal(maxTotal); // 同路由的并发数 pollingConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setConnectionManager(pollingConnectionManager); // 重试次数,默认是3次,没有开启 httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, requestSentRetryEnabled)); // 保持长连接配置,需要在头添加Keep-Alive httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()); List<Header> headers = new ArrayList<>(); // 参考:http://www.cnblogs.com/sink_cup/archive/2011/03/15/http_user_agent.html // headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36")); // 不需要设置,可能会乱码,请参考:http://www.crifan.com/set_accept_encoding_header_to_gzip_deflate_return_messy_code/ // headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate")); // headers.add(new BasicHeader("Accept-Language", "zh-CN")); headers.add(new BasicHeader("Connection", "Keep-Alive")); httpClientBuilder.setDefaultHeaders(headers); HttpClient httpClient = httpClientBuilder.build(); // httpClient连接配置,底层是配置RequestConfig HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); // 连接超时 clientHttpRequestFactory.setConnectTimeout(connectTimeout); // 数据读取超时时间,即SocketTimeout clientHttpRequestFactory.setReadTimeout(readTimeout); // 连接不够用的等待时间,不宜过长,必须设置,比如连接不够用时,时间过长将是灾难性的 clientHttpRequestFactory.setConnectionRequestTimeout(connectionRequestTimeout); // 缓冲请求数据,默认值是true。通过POST或者PUT大量发送数据时,建议将此属性更改为false,以免耗尽内存。 if (!bufferRequestBody) { clientHttpRequestFactory.setBufferRequestBody(false); } // 添加内容转换器 // List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(); messageConverters.add(new FastJsonHttpMessageConverter()); messageConverters.add(new StringHttpMessageConverter(Charset.forName(charset))); messageConverters.add(new ByteArrayHttpMessageConverter()); super.setMessageConverters(messageConverters); super.setRequestFactory(clientHttpRequestFactory); super.setErrorHandler(new DefaultResponseErrorHandler()); }
protected ExecCallbackAsyncREST<HttpResponse> buildAsyncClient(RESTPool pool) throws IOException { SSLContext sslContext; try { sslContext = SSLContext.getDefault(); } catch (NoSuchAlgorithmException e) { throw new IOException(e); } Registry<SchemeIOSessionStrategy> socketRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create() .register("http", NoopIOSessionStrategy.INSTANCE) .register("https", new SSLIOSessionStrategy(sslContext, NoopHostnameVerifier.INSTANCE)) .build(); IOReactorConfig socketConfig = IOReactorConfig.custom() .setIoThreadCount(pool.getReactorThreadCount()) .setSoTimeout(new Long(pool.getSocketTimeout()).intValue()) .setTcpNoDelay(true) .setSoKeepAlive(true) .setSelectInterval(REACTOR_SELECT_INTERVAL) .build(); ConnectionConfig connectionConfig = ConnectionConfig.custom() .setCharset(StandardCharsets.UTF_8) .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE) .build(); RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(new Long(pool.getMaxPoolWait()).intValue()) .setConnectTimeout(new Long(pool.getConnectionTimeout()).intValue()) .setExpectContinueEnabled(pool.expectContinue()) .setRedirectsEnabled(false) .setStaleConnectionCheckEnabled(pool.getValidationOnInactivity() >= 0) .build(); NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory = new ManagedNHttpClientConnectionFactory( new org.apache.http.impl.nio.codecs.DefaultHttpRequestWriterFactory(), new org.apache.http.impl.nio.codecs.DefaultHttpResponseParserFactory(), HeapByteBufferAllocator.INSTANCE ); //TODO set validateAfterInactivity when supported PoolingNHttpClientConnectionManager ccm = new PoolingNHttpClientConnectionManager( new DefaultConnectingIOReactor(socketConfig), connFactory, socketRegistry, new SystemDefaultDnsResolver() ); ccm.setMaxTotal(pool.getMaxTotal()); ccm.setDefaultMaxPerRoute(pool.getMaxPerRoute()); ccm.setDefaultConnectionConfig(connectionConfig); HttpAsyncClientBuilder builder = HttpAsyncClients.custom() .setConnectionManager(ccm) .setDefaultRequestConfig(requestConfig) .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE) .disableCookieManagement(); IdleAsyncConnectionEvictor evictor = new IdleAsyncConnectionEvictor(ccm, pool.getEvictorSleep(), TimeUnit.MILLISECONDS, pool.getMaxIdleTime(), TimeUnit.MILLISECONDS); addProxy(pool, builder); handleRedirects(pool, builder); CloseableHttpAsyncClient servClient = builder.build(); servClient.start(); HTTPCClientMonitor monitor = pool.hasConnectionMetrics() ? new HTTPCAsyncClientMonitor(pool.getName(), ccm) : null; return new HTTPCAsyncClient(servClient, evictor, monitor); }
@Override protected ExecREST buildClient(RESTPool pool) throws IOException { SSLContext sslContext; try { sslContext = SSLContext.getDefault(); } catch (NoSuchAlgorithmException e) { throw new IOException(e); } Registry<ConnectionSocketFactory> socketRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE)) .build(); //TODO buffers size SocketConfig socketConfig = SocketConfig.custom() .setSoTimeout(new Long(pool.getSocketTimeout()).intValue()) .setTcpNoDelay(true) .setSoKeepAlive(true) .setSoReuseAddress(true) .build(); ConnectionConfig connectionConfig = ConnectionConfig.custom() .setCharset(StandardCharsets.UTF_8) .setMalformedInputAction(CodingErrorAction.IGNORE) .setUnmappableInputAction(CodingErrorAction.IGNORE) .build(); HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory( new DefaultHttpRequestWriterFactory(), new DefaultHttpResponseParserFactory() ); RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(new Long(pool.getMaxPoolWait()).intValue()) .setConnectTimeout(new Long(pool.getConnectionTimeout()).intValue()) .setExpectContinueEnabled(pool.expectContinue()) .build(); PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager(socketRegistry, connFactory); ccm.setMaxTotal(pool.getMaxTotal()); ccm.setDefaultMaxPerRoute(pool.getMaxPerRoute()); ccm.setDefaultSocketConfig(socketConfig); ccm.setDefaultConnectionConfig(connectionConfig); ccm.setValidateAfterInactivity(pool.getValidationOnInactivity()); HttpClientBuilder builder = HttpClients.custom() .setConnectionManager(ccm) .setDefaultRequestConfig(requestConfig) .disableAutomaticRetries() .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE) .disableCookieManagement() .disableContentCompression(); addProxy(pool, builder); handleRedirects(pool, builder); CloseableHttpClient servClient = builder.build(); IdleConnectionEvictor evictor = new IdleConnectionEvictor(ccm, pool.getEvictorSleep(), TimeUnit.MILLISECONDS, pool.getMaxIdleTime(), TimeUnit.MILLISECONDS); HTTPCClientMonitor monitor = pool.hasConnectionMetrics() ? new HTTPCSyncClientMonitor(pool.getName(), ccm) : null; return new HTTPCClient(servClient, evictor, monitor); }