/** * */ public DefaultConnector() { final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.closeIdleConnections(120, TimeUnit.SECONDS); // would be nice to set this from outside -> keep alive final SocketConfig sConfig = SocketConfig.custom().setSoKeepAlive(true).setSoTimeout(Context.SOCKET_TO).build(); cm.setDefaultSocketConfig(sConfig); cm.setMaxTotal(150); cm.setDefaultMaxPerRoute(150); cm.setValidateAfterInactivity(0); final HttpRequestRetryHandler rh = new DefaultHttpRequestRetryHandler(3, true); httpClient = HttpClients.custom().setRetryHandler(rh).setConnectionManager(cm).build(); }
public DefaultHttpClient getHttpClientInstance() { if (this.httpClient != null) { return this.httpClient; } HttpParams params = new BasicHttpParams(); params.setParameter("http.connection.timeout", CONNECTION_TIMEOUT); params.setParameter("http.socket.timeout", SOCKET_TIMEOUT); params.setParameter("http.protocol.version", HttpVersion.HTTP_1_1); params.setParameter("http.useragent", "Apache-HttpClient/Android"); params.setParameter("http.connection.stalecheck", Boolean.valueOf(false)); this.httpClient = new DefaultHttpClient(params); this.httpClient.addRequestInterceptor(new GzipHttpRequestInterceptor()); this.httpClient.addResponseInterceptor(new GzipHttpResponseInterceptor()); this.httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); return this.httpClient; }
public void performLogoutOnServiceProviders(String idpSessionId) { List<String> serviceProviderLogoutUrls = idpConfig.getServiceProviderLogoutUrls(); asyncServiceLogoutExecutor.submit(() -> { HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); HttpClient client = HttpClientBuilder.create() .setConnectionManager(connectionManager) .setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)) .build(); try { for (String serviceProviderLogoutUrl : serviceProviderLogoutUrls) { callLoggedOutServiceUrl(client, serviceProviderLogoutUrl, idpSessionId); } } catch (Throwable ex) { log.error("Unable to perform logout on IDP services for session {}", idpSessionId, ex); } finally { connectionManager.shutdown(); } }); }
private boolean ping() throws IOException { RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(HTTP_PING_TIMEOUT) .setConnectTimeout(HTTP_PING_TIMEOUT) .setConnectionRequestTimeout(HTTP_PING_TIMEOUT) .build(); CloseableHttpClient httpClient = HttpClientBuilder.create() .setDefaultRequestConfig(requestConfig) .setRetryHandler(new DefaultHttpRequestRetryHandler(HTTP_CLIENT_RETRIES, false)) .build(); try { CloseableHttpResponse response = httpClient.execute(RequestBuilder.create(method.toUpperCase()).setUri(url).build()); try { int responseCode = response.getStatusLine().getStatusCode(); if (responseCode == 501) { throw new IllegalArgumentException("Invalid or not supported HTTP method '" + method.toUpperCase() + "' for checking " + url); } return (responseCode >= statusMin && responseCode <= statusMax); } finally { response.close(); } } finally { httpClient.close(); } }
/** * Control retry handler * @param useRetry when false the client will not try to retry failed requests. */ public static void setUseRetry(final DefaultHttpClient httpClient, boolean useRetry) { if (!useRetry) { httpClient.setHttpRequestRetryHandler(NO_RETRY); } else { // if the request is not fully sent, we retry // streaming updates are not a problem, because they are not retryable httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(){ @Override protected boolean handleAsIdempotent(final HttpRequest request) { return false; // we can't tell if a Solr request is idempotent } }); } }
/** * Returns a new HTTP client by the specified repository configuration. * * @return a new HTTP client by the specified repository configuration */ private HttpClient createHttpClient() { RequestConfig.Builder requestBuilder = RequestConfig.custom().setSocketTimeout(10 * 1000); if (repoConfig.getProxy() != null) { requestBuilder.setProxy(HttpHost.create(repoConfig.getProxy())); } HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setDefaultRequestConfig(requestBuilder.build()); httpClientBuilder = httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3, false)); httpClientBuilder = httpClientBuilder.setRedirectStrategy(new DefaultRedirectStrategy()); if (repoConfig.getUsername() != null) { final Credentials creds = new UsernamePasswordCredentials(repoConfig.getUsername(), repoConfig.getPassword()); final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, creds); httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credsProvider); } return httpClientBuilder.build(); }
public OAuthHttpClient(int maxConnection, int connectTimeout, int socketTimeout) { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build()); // set max connection connectionManager.setMaxTotal(maxConnection); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(connectTimeout) .setSocketTimeout(socketTimeout) .setCookieSpec(CookieSpecs.IGNORE_COOKIES) .build(); httpClient = HttpClientBuilder.create() .setConnectionManager(connectionManager) .setDefaultRequestConfig(requestConfig) .setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)) .build(); }
protected HttpResponse doConnect(HttpTask task) throws Exception { mHttpParams = new BasicHttpParams(); HttpProtocolParams.setVersion(mHttpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(mHttpParams, "utf-8"); HttpConnectionParams.setConnectionTimeout(mHttpParams, task.getConnectTimeout()); HttpConnectionParams.setSoTimeout(mHttpParams, task.getSocketTimeout()); // 加入代理 HttpUtils.fillProxy(mContext, mHttpParams); mHttpClient = new DefaultHttpClient(mHttpParams); // 重试一次 mHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(2, true)); HttpResponse httpResponse = mHttpClient.execute(task.getHttpUriRequest()); return httpResponse; }
@Test public void test() throws Exception { DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(); IOException iOException = new IOException(); HttpContext context = new BasicHttpContext(); assertTrue(retryHandler.retryRequest(iOException, 1, context)); assertTrue(retryHandler.retryRequest(iOException, 2, context)); PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); verifier.printCache(); verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", DefaultHttpRequestRetryHandler.class.getMethod("retryRequest", IOException.class, int.class, HttpContext.class), annotation("http.internal.display", IOException.class.getName() + ", 1"), annotation("RETURN_DATA", true))); verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", DefaultHttpRequestRetryHandler.class.getMethod("retryRequest", IOException.class, int.class, HttpContext.class), annotation("http.internal.display", IOException.class.getName() + ", 2"), annotation("RETURN_DATA", true))); verifier.verifyTraceCount(0); }
/** * Returns a client with all our selected properties / params. * * @param timeout * - socket timeout to set * @return client */ public static final HttpClient getClient(int timeout) { HttpClientBuilder client = HttpClientBuilder.create(); // set the connection manager client.setConnectionManager(connectionManager); // dont retry client.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); // establish a connection within x seconds RequestConfig config = RequestConfig.custom().setSocketTimeout(timeout).build(); client.setDefaultRequestConfig(config); // no redirects client.disableRedirectHandling(); // set custom ua client.setUserAgent(userAgent); // set the proxy if the user has one set if ((System.getProperty("http.proxyHost") != null) && (System.getProperty("http.proxyPort") != null)) { HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost").toString(), Integer.valueOf(System.getProperty("http.proxyPort"))); client.setProxy(proxy); } return client.build(); }
/** * Returns a client with all our selected properties / params and SSL enabled. * * @return client */ public static final HttpClient getSecureClient() { HttpClientBuilder client = HttpClientBuilder.create(); // set the ssl verifier to accept all client.setSSLHostnameVerifier(new NoopHostnameVerifier()); // set the connection manager client.setConnectionManager(connectionManager); // dont retry client.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); // establish a connection within x seconds RequestConfig config = RequestConfig.custom().setSocketTimeout(connectionTimeout).build(); client.setDefaultRequestConfig(config); // no redirects client.disableRedirectHandling(); // set custom ua client.setUserAgent(userAgent); return client.build(); }
public synchronized HttpClient getClient() { if (m_client == null) { m_client = new DefaultHttpClient(); HttpParams clientParams = m_client.getParams(); clientParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, m_timeout); clientParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, m_timeout); clientParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); m_client.setParams(clientParams); m_client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(m_retries, false)); } return m_client; }
/** * Internal function to create http clients for fetching * @param timeoutms The timeout, which is applied to the connect, connection request, and socket timeouts. Ignored if <=0 * @param numRetries The number of retries. Ignored if <0 * @param credentials optional credentials provider to be used for authentication * @return A CloseableHttpClient with the specified parameters set */ private static CloseableHttpClient makeCloseableHttpClient(int timeoutms, int numRetries, Optional<CredentialsProvider> credentials) { HttpClientBuilder builder = HttpClientBuilder.create(); if(credentials.isPresent()) { builder.setDefaultCredentialsProvider(credentials.get()); } if (numRetries >= 0) { DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(numRetries, false); builder = builder.setRetryHandler(retryHandler); } if (timeoutms > 0) { RequestConfig config = RequestConfig.custom() .setConnectTimeout(timeoutms) .setConnectionRequestTimeout(timeoutms) .setSocketTimeout(timeoutms).build(); builder.setDefaultRequestConfig(config); } return builder.build(); }
public HttpStoreClientFactory(ClientConfig config) { super(config); ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(SchemeRegistryFactory.createDefault(), config.getConnectionTimeout(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS); mgr.setMaxTotal(config.getMaxTotalConnections()); mgr.setDefaultMaxPerRoute(config.getMaxConnectionsPerNode()); this.httpClient = new DefaultHttpClient(mgr); HttpParams clientParams = this.httpClient.getParams(); HttpProtocolParams.setUserAgent(clientParams, VOLDEMORT_USER_AGENT); HttpProtocolParams.setVersion(clientParams, HttpVersion.HTTP_1_1); HttpConnectionParams.setConnectionTimeout(clientParams, config.getConnectionTimeout(TimeUnit.MILLISECONDS)); HttpConnectionParams.setSoTimeout(clientParams, config.getSocketTimeout(TimeUnit.MILLISECONDS)); HttpConnectionParams.setStaleCheckingEnabled(clientParams, false); this.httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); HttpClientParams.setCookiePolicy(clientParams, CookiePolicy.IGNORE_COOKIES); this.reroute = config.getRoutingTier().equals(RoutingTier.SERVER); this.requestFormatFactory = new RequestFormatFactory(); }
/** * Returns a client with all our selected properties / params. * * @return client */ public static final DefaultHttpClient getClient() { // create a singular HttpClient object DefaultHttpClient client = new DefaultHttpClient(connectionManager); // dont retry client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); // get the params for the client HttpParams params = client.getParams(); // establish a connection within x seconds params.setParameter(CoreConnectionPNames.SO_TIMEOUT, connectionTimeout); // no redirects params.setParameter(ClientPNames.HANDLE_REDIRECTS, false); // set custom ua params.setParameter(CoreProtocolPNames.USER_AGENT, userAgent); // set the proxy if the user has one set if ((System.getProperty("http.proxyHost") != null) && (System.getProperty("http.proxyPort") != null)) { HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost").toString(), Integer.valueOf(System.getProperty("http.proxyPort"))); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } return client; }
private CloseableHttpClient getConnection() { CloseableHttpClient httpClient = HttpClients.custom() /* 设置连接池管理 */ .setConnectionManager(poolConnManager) /* 设置请求配置 */ .setDefaultRequestConfig(requestConfig) /* 设置重试次数 */ .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)).build(); if (poolConnManager != null && poolConnManager.getTotalStats() != null) { log.info("now client pool " + poolConnManager.getTotalStats().toString()); } return 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; }
/** * Create a thread-safe client. This client does not do redirecting, to allow us to capture * correct "error" codes. * * @return HttpClient */ public final DefaultHttpClient createHttpClient() { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); HttpProtocolParams.setUseExpectContinue(params, true); // Turn off stale checking. Our connections break all the time anyway, // and it's not worth it to pay the penalty of checking every time. HttpConnectionParams.setStaleCheckingEnabled(params, false); // Default connection and socket timeout of 30 seconds. Tweak to taste. HttpConnectionParams.setConnectionTimeout(params, 10*1000); HttpConnectionParams.setSoTimeout(params, 20*1000); HttpConnectionParams.setSocketBufferSize(params, 8192); ConnManagerParams.setTimeout(params, 5 * 1000); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(50)); ConnManagerParams.setMaxTotalConnections(params, 200); // Sets up the http part of the service. final SchemeRegistry supportedSchemes = new SchemeRegistry(); // Register the "http" protocol scheme, it is required // by the default operator to look up socket factories. final SocketFactory sf = PlainSocketFactory.getSocketFactory(); supportedSchemes.register(new Scheme("http", sf, 80)); supportedSchemes.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); final ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager(params, supportedSchemes); DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params); httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); return httpClient; }
/** * 多线程的httpclinet * @return */ private CloseableHttpClient getThredHttpClient() { //单线程跑是基本看不出配置了连接池的好处的,只有使用多线程爬取数据的时候,并且数据量越大效果越明显 PoolingHttpClientConnectionManager conMgr = new PoolingHttpClientConnectionManager(); conMgr.setMaxTotal(200); // 设置整个连接池最大连接数 根据自己的场景决定 // 是路由的默认最大连接(该值默认为2),限制数量实际使用DefaultMaxPerRoute并非MaxTotal。 // 设置过小无法支持大并发(ConnectionPoolTimeoutException: Timeout waiting for connection from pool),路由是对maxTotal的细分。 conMgr.setDefaultMaxPerRoute(conMgr.getMaxTotal());// (目前只有一个路由,因此让他等于最大值) //另外设置http client的重试次数,默认是3次;当前是禁用掉(如果项目量不到,这个默认即可) CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(conMgr). setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)).build(); // 另外设置http client的重试次数,默认是3次;当前是禁用掉(如果项目量不到,这个默认即可) RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000).setConnectionRequestTimeout(15000).setStaleConnectionCheckEnabled(true).build(); return httpClient; }
public CmmnHttpActivityBehaviorImpl() { org.flowable.cmmn.engine.HttpClientConfig config = CommandContextUtil.getCmmnEngineConfiguration().getHttpClientConfig(); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // https settings if (config.isDisableCertVerify()) { try { SSLContextBuilder builder = new SSLContextBuilder(); builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); httpClientBuilder.setSSLSocketFactory( new SSLConnectionSocketFactory(builder.build(), new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } })); } catch (Exception e) { LOGGER.error("Could not configure HTTP client SSL self signed strategy", e); } } // request retry settings int retryCount = 0; if (config.getRequestRetryLimit() > 0) { retryCount = config.getRequestRetryLimit(); } httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, false)); this.httpActivityExecutor = new HttpActivityExecutor(httpClientBuilder, new NopErrorPropagator()); }
public HttpActivityBehaviorImpl() { HttpClientConfig config = CommandContextUtil.getProcessEngineConfiguration().getHttpClientConfig(); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // https settings if (config.isDisableCertVerify()) { try { SSLContextBuilder builder = new SSLContextBuilder(); builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); httpClientBuilder.setSSLSocketFactory( new SSLConnectionSocketFactory(builder.build(), new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } })); } catch (Exception e) { LOGGER.error("Could not configure HTTP client SSL self signed strategy", e); } } // request retry settings int retryCount = 0; if (config.getRequestRetryLimit() > 0) { retryCount = config.getRequestRetryLimit(); } httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, false)); this.httpActivityExecutor = new HttpActivityExecutor(httpClientBuilder, new ProcessErrorPropagator()); }
private CloseableHttpClient buildHttpClient(NomadApiConfiguration config) { return HttpClientBuilder.create() .setRetryHandler(new DefaultHttpRequestRetryHandler() { @Override protected boolean handleAsIdempotent(HttpRequest request) { return true; } }) .setSSLContext(buildSslContext(config.getTls())) .setSSLHostnameVerifier(new NomadHostnameVerifier()) .build(); }
/** * Creates a HttpClient to use for making requests. * * @param metadata The map containing the configuration for this client. * @return The created HttpClient object. */ private HttpClient createClient(Map<String, String> metadata) { int timeout = Integer.valueOf(metadata.getOrDefault(METADATA_TIMEOUT_KEY, String.valueOf(defaultTimeout))); int retries = Integer.valueOf(metadata.getOrDefault(METADATA_RETRY_KEY, String.valueOf(defaultRetries))); RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout) .setSocketTimeout(timeout).build(); return HttpClientBuilder.create() .setDefaultRequestConfig(config) .setRetryHandler(new DefaultHttpRequestRetryHandler(retries, false)) .build(); }
private CloseableHttpClient createHttpClient(ProxySettings proxySettings) { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create() /* set user agent string */ .setUserAgent(USER_AGENT) /* redirect on POSTs, too */ .setRedirectStrategy(new LaxRedirectStrategy()) /* increase max total connections from 20 to 200 */ .setMaxConnTotal(200) /* increate max connections per route from 2 to 200 (we only have one route) */ .setMaxConnPerRoute(200) /* set retry handler with maximum 3 retries and requestSentRetryEnabled=true so that even POST requests are retried (default is false) */ .setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)) .setDefaultCookieStore(cookieStore) .setDefaultRequestConfig(RequestConfig.custom() /* timeout in milliseconds until a connection is established, in ms */ .setConnectTimeout(SOCKET_TIMEOUT * 1000) /* socket timeout in milliseconds, which is the timeout for waiting for data */ .setSocketTimeout(SOCKET_TIMEOUT * 1000) /* don't use "Expect: 100-continue" because some proxies don't understand */ .setExpectContinueEnabled(false) /* need to relax default cookie policy to avoid problem with cookies with invalid expiry dates */ .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY) .build()); if (proxySettings != null) { httpClientBuilder.setProxy(new HttpHost(proxySettings.getHost(), proxySettings.getPort())); } return httpClientBuilder.build(); }
/** * Builds the retry handler if {@link #RETRY_SOCKET_EXCEPTION_PROPERTY} is true in the project's configuration. * * @return the HttpRequestRetryHandler or null depending on configuration */ protected HttpRequestRetryHandler buildRetryHandler() { // If configured to do so, allow the client to retry once if (ConfigContext.getCurrentContextConfig().getBooleanProperty(RETRY_SOCKET_EXCEPTION_PROPERTY, false)) { return new DefaultHttpRequestRetryHandler(1, true); } return null; }
private CloseableHttpClient generateClient(Site site) { HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(connectionManager); if (site != null && site.getUserAgent() != null) { httpClientBuilder.setUserAgent(site.getUserAgent()); } else { httpClientBuilder.setUserAgent(""); } if (site == null || site.isUseGzip()) { httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() { public void process( final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } } }); } SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build(); httpClientBuilder.setDefaultSocketConfig(socketConfig); if (site != null) { httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(site.getRetryTimes(), true)); } generateCookie(httpClientBuilder, site); return httpClientBuilder.build(); }
/** * Number of retry attempts. Default is 3 retries. * * @param retryCount Number of retry attempts. 0 means no retries. */ public HttpClientConfigurator retry(int retryCount, boolean requestSentRetryEnabled) { if (retryCount == 0) { builder.disableAutomaticRetries(); } else { builder.setRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, requestSentRetryEnabled)); } return this; }
public StreamClientImpl(StreamClientConfigurationImpl configuration) throws InitializationException { this.configuration = configuration; HttpProtocolParams.setContentCharset(globalParams, getConfiguration().getContentCharset()); HttpProtocolParams.setUseExpectContinue(globalParams, false); // These are some safety settings, we should never run into these timeouts as we // do our own expiration checking HttpConnectionParams.setConnectionTimeout(globalParams, (getConfiguration().getTimeoutSeconds()+5) * 1000); HttpConnectionParams.setSoTimeout(globalParams, (getConfiguration().getTimeoutSeconds()+5) * 1000); HttpConnectionParams.setStaleCheckingEnabled(globalParams, getConfiguration().getStaleCheckingEnabled()); if (getConfiguration().getSocketBufferSize() != -1) HttpConnectionParams.setSocketBufferSize(globalParams, getConfiguration().getSocketBufferSize()); // Only register 80, not 443 and SSL SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); clientConnectionManager = new PoolingClientConnectionManager(registry); clientConnectionManager.setMaxTotal(getConfiguration().getMaxTotalConnections()); clientConnectionManager.setDefaultMaxPerRoute(getConfiguration().getMaxTotalPerRoute()); httpClient = new DefaultHttpClient(clientConnectionManager, globalParams); if (getConfiguration().getRequestRetryCount() != -1) { httpClient.setHttpRequestRetryHandler( new DefaultHttpRequestRetryHandler(getConfiguration().getRequestRetryCount(), false) ); } }
public static HttpClient createHttpClient() { HttpClient result = null; try { // result = HttpClientBuilder.create().build(); // result = HttpClients.createDefault(); // HttpClientBuilder HttpClientBuilder builder = HttpClientBuilder.create(); builder.setMaxConnPerRoute(100); builder.setMaxConnTotal(200); builder.setRetryHandler(new DefaultHttpRequestRetryHandler()); // RequestConfig RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectTimeout(5 * 1000); requestConfigBuilder.setSocketTimeout(5 * 1000); // RequestConfig requestConfig = requestConfigBuilder.build(); builder.setDefaultRequestConfig(requestConfig); // ConnectionConfig ConnectionConfig.Builder connectionConfigBuilder = ConnectionConfig .custom(); connectionConfigBuilder.setBufferSize(8192); connectionConfigBuilder.setCharset(Charset.forName("UTF-8")); // ConnectionConfig connectionConfig = connectionConfigBuilder.build(); builder.setDefaultConnectionConfig(connectionConfig); // result = builder.build(); } catch (Exception ex) { ex.printStackTrace(); } return result; }
@Test public void test() { DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(); IOException iOException = new IOException(); HttpContext context = new BasicHttpContext(); assertTrue(retryHandler.retryRequest(iOException, 1, context)); assertTrue(retryHandler.retryRequest(iOException, 2, context)); assertEquals(2, getCurrentSpanEvents().size()); }
private DefaultHttpClient createHttpClientByte() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, KeyManagementException { HttpParams httpParams = createHttpParams(); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schReg.register(new Scheme("https", sf, 443)); ThreadSafeClientConnManager tccm = new ThreadSafeClientConnManager(httpParams, schReg); DefaultHttpClient client = new DefaultHttpClient(tccm, httpParams); client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); return client; }
private DefaultHttpClient createHttpClient() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, KeyManagementException { HttpParams httpParams = createHttpParams(); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schReg.register(new Scheme("https", sf, 443)); ClientConnectionManager conManager = new ThreadSafeClientConnManager(httpParams, schReg); DefaultHttpClient client = new DefaultHttpClient(conManager, httpParams); client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); return client; }
private String getResponseViaHttp(String hostname) throws Exception { String url = serverUrl; url = url.replace("{hostname}", hostname); if (Logger.isDebugEnabled()) { Logger.debug("Making http call to url: " + url); } DefaultHttpClient client = new DefaultHttpClient(); client.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 2000); client.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 5000); DefaultHttpRequestRetryHandler retryhandler = new DefaultHttpRequestRetryHandler(NUM_RETRIER_ACROSS_NODES, true); client.setHttpRequestRetryHandler(retryhandler); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); int statusCode = response.getStatusLine().getStatusCode(); if (!(statusCode == 200)) { Logger.error("Got non 200 status code from " + url); return null; } InputStream in = null; try { in = response.getEntity().getContent(); return IOUtilities.toString(in); } finally { if (in != null) { in.close(); } } }
private static HttpClient newDefaultHttpClient(SSLSocketFactory socketFactory, HttpParams params, ProxySelector proxySelector) { SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", socketFactory, 443)); ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(params, registry); DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, params); httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); httpClient.setRoutePlanner(new ProxySelectorRoutePlanner(registry, proxySelector)); return httpClient; }
/** * @param connectionManager the connection manager. * @return the http client to use backed by a pooling connection manager. */ @Bean(destroyMethod = "close") public CloseableHttpClient getHttpClient(PoolingHttpClientConnectionManager connectionManager) { return HttpClientBuilder.create() // .setConnectionManager(connectionManager) // .setKeepAliveStrategy(getKeepAliveStrategy()) // .setDefaultRequestConfig(getRequestConfig()) // .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)) // .setRedirectStrategy(new RedirectTransferStrategy()) // .build(); }
/** * Control retry handler * @param useRetry when false the client will not try to retry failed requests. */ public static void setUseRetry(final DefaultHttpClient httpClient, boolean useRetry) { if (!useRetry) { httpClient.setHttpRequestRetryHandler(NO_RETRY); } else { httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler()); } }
private DefaultHttpClient createHttpClientByte() { final SchemeRegistry supportedSchemes = new SchemeRegistry(); supportedSchemes.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), HTTP_PORT)); supportedSchemes.register(new Scheme("https", EasySSLSocketFactory.getSocketFactory(), 443)); final HttpParams httpParams = createHttpParams(); final ThreadSafeClientConnManager tccm = new ThreadSafeClientConnManager(httpParams, supportedSchemes); DefaultHttpClient client = new DefaultHttpClient(tccm, httpParams); client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); return client; }
private DefaultHttpClient createHttpClient() { final SchemeRegistry supportedSchemes = new SchemeRegistry(); supportedSchemes.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); supportedSchemes.register(new Scheme("https", EasySSLSocketFactory.getSocketFactory(), 443)); final HttpParams httpParams = createHttpParams(); final ThreadSafeClientConnManager tccm = new ThreadSafeClientConnManager(httpParams, supportedSchemes); DefaultHttpClient client = new DefaultHttpClient(tccm, httpParams); client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); return client; }