Java Code Examples for org.apache.http.impl.client.DefaultHttpClient#setHttpRequestRetryHandler()
The following examples show how to use
org.apache.http.impl.client.DefaultHttpClient#setHttpRequestRetryHandler() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpLoadTestClient.java From carbon-apimgt with Apache License 2.0 | 6 votes |
@Override public void run() { SchemeRegistry supportedSchemes = new SchemeRegistry(); SocketFactory sf = PlainSocketFactory.getSocketFactory(); supportedSchemes.register(new Scheme("http", sf, 80)); ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(supportedSchemes); connManager.setDefaultMaxPerRoute(1000); DefaultHttpClient client = new DefaultHttpClient(connManager); HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 30000); HttpConnectionParams.setSoTimeout(params, 30000); client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { public boolean retryRequest(IOException e, int i, HttpContext httpContext) { return false; } }); //test API call long t1 = System.currentTimeMillis(); testEndpoint(client,apiEndpoint); long t2 = System.currentTimeMillis(); timeElapsedForAPICall = t2 - t1; }
Example 2
Source File: AbstractGoogleClientFactory.java From nexus-blobstore-google-cloud with Eclipse Public License 1.0 | 6 votes |
/** * Replicates {@link ApacheHttpTransport#newDefaultHttpClient()} with one exception: * * 1 retry is allowed. * * @see DefaultHttpRequestRetryHandler */ DefaultHttpClient 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 defaultHttpClient = new DefaultHttpClient(connectionManager, params); // retry only once defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(1, true)); if (proxySelector != null) { defaultHttpClient.setRoutePlanner(new ProxySelectorRoutePlanner(registry, proxySelector)); } defaultHttpClient.setKeepAliveStrategy((response, context) -> KEEP_ALIVE_DURATION); return defaultHttpClient; }
Example 3
Source File: AsyncHttpClient.java From MiBandDecompiled with Apache License 2.0 | 6 votes |
public AsyncHttpClient(SchemeRegistry schemeregistry) { a = 10; b = 10000; h = true; BasicHttpParams basichttpparams = new BasicHttpParams(); ConnManagerParams.setTimeout(basichttpparams, b); ConnManagerParams.setMaxConnectionsPerRoute(basichttpparams, new ConnPerRouteBean(a)); ConnManagerParams.setMaxTotalConnections(basichttpparams, 10); HttpConnectionParams.setSoTimeout(basichttpparams, b); HttpConnectionParams.setConnectionTimeout(basichttpparams, b); HttpConnectionParams.setTcpNoDelay(basichttpparams, true); HttpConnectionParams.setSocketBufferSize(basichttpparams, 8192); HttpProtocolParams.setVersion(basichttpparams, HttpVersion.HTTP_1_1); ThreadSafeClientConnManager threadsafeclientconnmanager = new ThreadSafeClientConnManager(basichttpparams, schemeregistry); e = getDefaultThreadPool(); f = new WeakHashMap(); g = new HashMap(); d = new SyncBasicHttpContext(new BasicHttpContext()); c = new DefaultHttpClient(threadsafeclientconnmanager, basichttpparams); c.addRequestInterceptor(new a(this)); c.addResponseInterceptor(new b(this)); c.addRequestInterceptor(new c(this), 0); c.setHttpRequestRetryHandler(new z(5, 1500)); }
Example 4
Source File: HttpClientUtil.java From AndroidRobot with Apache License 2.0 | 6 votes |
/** * 获取DefaultHttpClient对象 * * @param charset * 字符编码 * @return DefaultHttpClient对象 */ private static DefaultHttpClient getDefaultHttpClient(final String charset) { DefaultHttpClient httpclient = new DefaultHttpClient(); // 模拟浏览器,解决一些服务器程序只允许浏览器访问的问题 httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT); httpclient.getParams().setParameter( CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE); httpclient.getParams().setParameter( CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset == null ? CHARSET_ENCODING : charset); // 浏览器兼容性 httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // 定义重试策略 httpclient.setHttpRequestRetryHandler(requestRetryHandler); return httpclient; }
Example 5
Source File: SOAPClient.java From micro-integrator with Apache License 2.0 | 6 votes |
/** * Sets up the client */ private void setup() { httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager()); HttpParams params = httpclient.getParams(); HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT); httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { public boolean retryRequest(IOException e, int i, HttpContext httpContext) { return false; } }); }
Example 6
Source File: SOAPClient.java From product-ei with Apache License 2.0 | 5 votes |
/** * Sets up the client */ private void setup() { httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager()); HttpParams params = httpclient.getParams(); HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT); httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { public boolean retryRequest(IOException e, int i, HttpContext httpContext) { return false; } }); }
Example 7
Source File: HttpEndpointBasedTokenMapSupplier.java From dyno with Apache License 2.0 | 5 votes |
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(); } } }
Example 8
Source File: ApacheHttpTransport.java From google-http-java-client with Apache License 2.0 | 5 votes |
/** * Creates a new instance of the Apache HTTP client that is used by the {@link * #ApacheHttpTransport()} constructor. * * @param socketFactory SSL socket factory * @param params HTTP parameters * @param proxySelector HTTP proxy selector to use {@link ProxySelectorRoutePlanner} or {@code * null} for {@link DefaultHttpRoutePlanner} * @return new instance of the Apache HTTP client */ static DefaultHttpClient newDefaultHttpClient( SSLSocketFactory socketFactory, HttpParams params, ProxySelector proxySelector) { // See http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html 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 defaultHttpClient = new DefaultHttpClient(connectionManager, params); defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); if (proxySelector != null) { defaultHttpClient.setRoutePlanner(new ProxySelectorRoutePlanner(registry, proxySelector)); } return defaultHttpClient; }
Example 9
Source File: NetworkHelper.java From fanfouapp-opensource with Apache License 2.0 | 5 votes |
public final static DefaultHttpClient createHttpClient(final Context context) { final HttpParams params = NetworkHelper.createHttpParams(); final DefaultHttpClient client = new DefaultHttpClient(params); client.addRequestInterceptor(new GzipRequestInterceptor()); client.addResponseInterceptor(new GzipResponseInterceptor()); client.setHttpRequestRetryHandler(new RequestRetryHandler( NetworkHelper.MAX_RETRY_TIMES)); NetworkHelper.checkAndSetProxy(context, params); return client; }
Example 10
Source File: HttpClientConfigurer.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
private void configureRetryHandler(DefaultHttpClient httpClient) { httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { return false; } }); }
Example 11
Source File: HttpClientConfigurer.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
private void configureRetryHandler(DefaultHttpClient httpClient) { httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { return false; } }); }
Example 12
Source File: BuddycloudHTTPHelper.java From buddycloud-android with Apache License 2.0 | 5 votes |
public static HttpClient createHttpClient(Context context) { try { SchemeRegistry registry = new SchemeRegistry(); SocketFactory socketFactory = createSecureSocketFactory(); registry.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80)); registry.register(new Scheme("https", socketFactory, 443)); HttpParams connManagerParams = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(connManagerParams, 20); ConnManagerParams.setMaxConnectionsPerRoute(connManagerParams, new ConnPerRouteBean(20)); ClientConnectionManager ccm = new ThreadSafeClientConnManager( connManagerParams, registry); DefaultHttpClient client = new DefaultHttpClient(ccm, null); client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler( 0, true)); return client; } catch (Exception e) { throw new RuntimeException(e); } }
Example 13
Source File: StreamClientImpl.java From TVRemoteIME with GNU General Public License v2.0 | 5 votes |
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) ); } }
Example 14
Source File: DatabricksRestClientImpl425.java From databricks-rest-client with Apache License 2.0 | 5 votes |
protected void initClient(DatabricksServiceFactory.Builder builder) { try { SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); sslContext.init(null, null, new SecureRandom()); SSLSocketFactory sf = new SSLSocketFactory(sslContext); Scheme httpsScheme = new Scheme("https", HTTPS_PORT, sf); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(httpsScheme); ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry); HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, builder.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(params, builder.getSoTimeout()); DefaultHttpClient defaultHttpClient = new DefaultHttpClient(cm, params); defaultHttpClient.setHttpRequestRetryHandler(retryHandler); // set authorization header if token base if (isNotEmpty(builder.getToken())) { isTokenAuth = true; authToken = builder.getToken(); } else if (isNotEmpty(builder.getUsername()) && isNotEmpty(builder.getPassword())) { defaultHttpClient.getCredentialsProvider().setCredentials( new AuthScope(host, HTTPS_PORT), new UsernamePasswordCredentials(builder.getUsername(), builder.getPassword())); } client = new AutoRetryHttpClient(defaultHttpClient, retryStrategy); } catch (Exception e) { logger.error("", e); } url = String.format("https://%s/api/%s", host, apiVersion); mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); }
Example 15
Source File: HttpClientConfigurer.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
private void configureRetryHandler(DefaultHttpClient httpClient) { httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { return false; } }); }
Example 16
Source File: HttpClientConfigurer.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
private void configureRetryHandler(DefaultHttpClient httpClient) { httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { return false; } }); }
Example 17
Source File: StreamClientImpl.java From openhab1-addons with Eclipse Public License 2.0 | 4 votes |
public StreamClientImpl(StreamClientConfigurationImpl configuration) throws InitializationException { this.configuration = configuration; ConnManagerParams.setMaxTotalConnections(globalParams, getConfiguration().getMaxTotalConnections()); HttpConnectionParams.setConnectionTimeout(globalParams, getConfiguration().getConnectionTimeoutSeconds() * 1000); HttpConnectionParams.setSoTimeout(globalParams, getConfiguration().getDataReadTimeoutSeconds() * 1000); HttpProtocolParams.setContentCharset(globalParams, getConfiguration().getContentCharset()); if (getConfiguration().getSocketBufferSize() != -1) { // Android configuration will set this to 8192 as its httpclient is based // on a random pre 4.0.1 snapshot whose BasicHttpParams do not set a default value for socket buffer size. // This will also avoid OOM on the HTC Thunderbolt where default size is 2Mb (!): // http://stackoverflow.com/questions/5358014/android-httpclient-oom-on-4g-lte-htc-thunderbolt HttpConnectionParams.setSocketBufferSize(globalParams, getConfiguration().getSocketBufferSize()); } HttpConnectionParams.setStaleCheckingEnabled(globalParams, getConfiguration().getStaleCheckingEnabled()); // This is a pretty stupid API... https://issues.apache.org/jira/browse/HTTPCLIENT-805 SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); // The 80 here is... useless clientConnectionManager = new ThreadSafeClientConnManager(globalParams, registry); httpClient = new DefaultHttpClient(clientConnectionManager, globalParams); if (getConfiguration().getRequestRetryCount() != -1) { httpClient.setHttpRequestRetryHandler( new DefaultHttpRequestRetryHandler(getConfiguration().getRequestRetryCount(), false)); } /* * // TODO: Ugh! And it turns out that by default it doesn't even use persistent connections properly! * * @Override * protected ConnectionReuseStrategy createConnectionReuseStrategy() { * return new NoConnectionReuseStrategy(); * } * * @Override * protected ConnectionKeepAliveStrategy createConnectionKeepAliveStrategy() { * return new ConnectionKeepAliveStrategy() { * public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) { * return 0; * } * }; * } * httpClient.removeRequestInterceptorByClass(RequestConnControl.class); */ }
Example 18
Source File: HttpRequestHelper.java From YiBo with Apache License 2.0 | 4 votes |
private static synchronized HttpClient createHttpClient(HttpConfig config) { if (config == null) { return null; } if (connectionManager == null) { connectionManager = createConnectionManager(); } HttpParams httpParams = new BasicHttpParams(); if (config.getHttpConnectionTimeout() > 0) { httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getHttpConnectionTimeout()); } if (config.getHttpReadTimeout() > 0) { httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getHttpReadTimeout()); } // 设置cookie策略 httpParams.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // 设置http.protocol.expect-continue参数为false,即不使用Expect:100-Continue握手, // 因为如果服务器不支持HTTP 1.1,则会导致HTTP 417错误。 HttpProtocolParams.setUseExpectContinue(httpParams, false); // 设置User-Agent HttpProtocolParams.setUserAgent(httpParams, config.getUserAgent()); // 设置HTTP版本为 HTTP 1.1 HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); DefaultHttpClient httpClient = new LibHttpClient(connectionManager, httpParams); updateProxySetting(config, httpClient); if (config.isUseGzip()) { httpClient.addRequestInterceptor(new GzipRequestInterceptor()); httpClient.addResponseInterceptor(new GzipResponseInterceptor()); } if (config.getHttpRetryCount() > 0) { HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(config.getHttpRetryCount(), true); httpClient.setHttpRequestRetryHandler(retryHandler); } return httpClient; }
Example 19
Source File: Kick.java From freeacs with MIT License | 4 votes |
protected KickResponse kickUsingTCP(Unit unit, String crUrl, String crPass, String crUser) throws MalformedURLException { DefaultHttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(crUrl); get.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000); get.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, true)); int statusCode; if (crUser != null && crPass != null) { get = authenticate(client, get, crUrl, crUser, crPass); log.debug( unit.getId() + " had a password and username, hence the kick will be executed with authentication (digest/basic)"); } try { HttpResponse response = client.execute(get); statusCode = response.getStatusLine().getStatusCode(); } catch (ConnectTimeoutException ce) { log.warn(unit.getId() + " did not respond, indicating a NAT problem or disconnected."); return new KickResponse( false, "TCP/HTTP-kick to " + crUrl + " failed, probably due to NAT or other connection problems: " + ce.getMessage()); } catch (Throwable t) { log.warn(unit.getId() + " did not respond, an error has occured: ", t); return new KickResponse( false, "TCP/HTTP-kick to " + crUrl + " failed because of an unexpected error: " + t.getMessage()); } if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_NO_CONTENT) { log.debug( unit.getId() + " responded with HTTP " + statusCode + ", indicating a successful kick"); return new KickResponse( true, "TCP/HTTP-kick to " + crUrl + " got HTTP response code " + statusCode + ", indicating success"); } else { log.warn( unit.getId() + " responded with HTTP " + statusCode + ", indicating a unsuccessful kick"); if (statusCode == HttpStatus.SC_FORBIDDEN || statusCode == HttpStatus.SC_UNAUTHORIZED) { return new KickResponse( false, "TCP/HTTP-kick to " + crUrl + " (user:" + crUser + ",pass:" + crPass + ") failed, probably due to wrong user/pass since HTTP response code is " + statusCode); } else { return new KickResponse( false, "TCP/HTTP-kick to " + crUrl + " failed with HTTP response code " + statusCode); } } }
Example 20
Source File: RequestRetryHttpClientFactory.java From olingo-odata4 with Apache License 2.0 | 4 votes |
@Override public DefaultHttpClient create(final HttpMethod method, final URI uri) { final HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() { @Override public boolean retryRequest(final IOException exception, final int executionCount, final HttpContext context) { if (executionCount >= 5) { // Do not retry if over max retry count return false; } if (exception instanceof InterruptedIOException) { // Timeout return false; } if (exception instanceof UnknownHostException) { // Unknown host return false; } if (exception instanceof ConnectException) { // Connection refused return false; } if (exception instanceof SSLException) { // SSL handshake exception return false; } final HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }; final DefaultHttpClient httpClient = super.create(method, uri); httpClient.setHttpRequestRetryHandler(myRetryHandler); return httpClient; }