List of usage examples for org.apache.http.client HttpRequestRetryHandler HttpRequestRetryHandler
HttpRequestRetryHandler
From source file:cn.wanghaomiao.seimi.http.hc.HttpClientFactory.java
public static HttpClientBuilder cliBuilder(int timeout) { HttpRequestRetryHandler retryHander = new HttpRequestRetryHandler() { @Override/* w w w . j a va 2 s. c o m*/ public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount > 3) { // Do not retry if over max retry count return false; } if (exception instanceof java.net.SocketTimeoutException) { //? return true; } if (exception instanceof InterruptedIOException) { // Timeout return true; } if (exception instanceof UnknownHostException) { // Unknown host return false; } if (exception instanceof SSLException) { // SSL handshake exception return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }; RedirectStrategy redirectStrategy = new SeimiRedirectStrategy(); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout) .build(); PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = HttpClientConnectionManagerProvider .getHcPoolInstance(); return HttpClients.custom().setDefaultRequestConfig(requestConfig) .setConnectionManager(poolingHttpClientConnectionManager).setRedirectStrategy(redirectStrategy) .setRetryHandler(retryHander); }
From source file:org.tinymediamanager.scraper.util.TmmHttpClient.java
/** * instantiates a new CloseableHttpClient * //from www . j a v a 2s . com * @return CloseableHttpClient */ public static CloseableHttpClient createHttpClient() { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); // Increase default max connection per route to 5 connectionManager.setMaxTotal(5); HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); httpClientBuilder.setConnectionManager(connectionManager); // my own retry handler HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, 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 ConnectTimeoutException) { // Connection refused return false; } if (exception instanceof SSLException) { // SSL handshake exception return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }; httpClientBuilder.setRetryHandler(myRetryHandler); // set proxy if needed if ((Globals.settings.useProxy())) { setProxy(httpClientBuilder); } return httpClientBuilder.build(); }
From source file:com.mobicage.rogerthat.util.http.HTTPUtil.java
public static HttpClient getHttpClient(int connectionTimeout, int socketTimeout, final int retryCount) { final HttpParams params = new BasicHttpParams(); HttpConnectionParams.setStaleCheckingEnabled(params, true); HttpConnectionParams.setConnectionTimeout(params, connectionTimeout); HttpConnectionParams.setSoTimeout(params, socketTimeout); HttpClientParams.setRedirecting(params, false); final DefaultHttpClient httpClient = new DefaultHttpClient(params); if (shouldUseTruststore()) { KeyStore trustStore = loadTrustStore(); SSLSocketFactory socketFactory; try {//from ww w . ja v a 2 s.com socketFactory = new SSLSocketFactory(null, null, trustStore); } catch (Exception e) { throw new RuntimeException(e); } socketFactory.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); Scheme sch = new Scheme("https", socketFactory, CloudConstants.HTTPS_PORT); httpClient.getConnectionManager().getSchemeRegistry().register(sch); } if (retryCount > 0) { httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { return executionCount < retryCount; } }); } return httpClient; }
From source file:at.ac.tuwien.big.testsuite.impl.util.HttpClientProducer.java
@Produces @ApplicationScoped/*w w w. j a v a 2 s .c o m*/ HttpClient produceHttpClient() { PoolingClientConnectionManager cm = new PoolingClientConnectionManager(); cm.setMaxTotal(100); cm.setDefaultMaxPerRoute(20); DefaultHttpClient client = new DefaultHttpClient(cm); client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= MAX_RETRY_COUNT) { // Do not retry if over max retry count return false; } if (exception instanceof InterruptedIOException) { // Timeout return true; } if (exception instanceof UnknownHostException) { // Unknown host return true; } if (exception instanceof ConnectException) { // Connection refused return true; } if (exception instanceof SSLException) { // SSL handshake exception return true; } 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; } }); return new DecompressingHttpClient(client); }
From source file:org.apache.olingo.samples.client.core.http.RequestRetryHttpClientFactory.java
@Override public DefaultHttpClient create(final HttpMethod method, final URI uri) { final HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() { @Override/*from ww w.ja v a 2 s .c o m*/ 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; }
From source file:org.wso2.carbon.automation.extensions.servers.httpserver.SimpleHttpClient.java
public SimpleHttpClient() { this.client = new DefaultHttpClient(new ThreadSafeClientConnManager()); 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; }// w w w .j a va2 s .com }); }
From source file:com.johnson.grab.browser.HttpClientHolder.java
private static void init() { // custom builder builder = HttpClients.custom();//from w ww. j av a2 s. co m PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(); manager.setDefaultMaxPerRoute(DEFAULT_MAX_NUM_PER_ROUTE); manager.setMaxTotal(DEFAULT_MAX_TOTAL_NUM); builder.setConnectionManager(manager); builder.setMaxConnPerRoute(DEFAULT_MAX_NUM_PER_ROUTE); builder.setMaxConnTotal(DEFAULT_MAX_TOTAL_NUM); // headers List<Header> headers = new ArrayList<Header>(); headers.add(new BasicHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg," + " application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint," + " application/msword, */*")); headers.add(new BasicHeader("Accept-Language", "zh-cn,en-us,zh-tw,en-gb,en;")); headers.add(new BasicHeader("Accept-Charset", "gbk,gb2312,utf-8,BIG5,ISO-8859-1;")); headers.add(new BasicHeader("Connection", "Close")); headers.add(new BasicHeader("Cache-Control", "no-cache")); headers.add(new BasicHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; CIBA)")); headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate,sdch")); headers.add(new BasicHeader("Connection", "close")); builder.setDefaultHeaders(headers); // retry handler builder.setRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= MAX_EXECUTION_NUM) { return false; } if (exception instanceof ConnectTimeoutException) { return false; } if (exception instanceof InterruptedIOException) { return false; } if (exception instanceof UnknownHostException) { return false; } if (exception instanceof NoHttpResponseException) { return false; } if (exception instanceof SSLException) { return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); if (!(request instanceof HttpEntityEnclosingRequest)) { return true; } return false; } }); // build client client = builder.build(); }
From source file:tv.icntv.common.HttpClientHolder.java
private static void init() { // custom builder builder = HttpClients.custom();/*from w w w .j a v a 2s . c o m*/ PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(); manager.setDefaultMaxPerRoute(DEFAULT_MAX_NUM_PER_ROUTE); manager.setMaxTotal(DEFAULT_MAX_TOTAL_NUM); builder.setConnectionManager(manager); builder.setMaxConnPerRoute(DEFAULT_MAX_NUM_PER_ROUTE); builder.setMaxConnTotal(DEFAULT_MAX_TOTAL_NUM); // headers List<Header> headers = new ArrayList<Header>(); headers.add(new BasicHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg," + " application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint," + " application/msword, */*")); // headers.add(new BasicHeader("Content-Type","application/xml")); headers.add(new BasicHeader("Accept-Language", "zh-cn,en-us,zh-tw,en-gb,en;")); headers.add(new BasicHeader("Accept-Charset", "gbk,gb2312,utf-8,BIG5,ISO-8859-1;")); headers.add(new BasicHeader("Connection", "Close")); headers.add(new BasicHeader("Cache-Control", "no-cache")); headers.add(new BasicHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; CIBA)")); headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate,sdch")); headers.add(new BasicHeader("Connection", "close")); builder.setDefaultHeaders(headers); // retry handler builder.setRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= MAX_EXECUTION_NUM) { return false; } if (exception instanceof ConnectTimeoutException) { return false; } if (exception instanceof InterruptedIOException) { return false; } if (exception instanceof UnknownHostException) { return false; } if (exception instanceof NoHttpResponseException) { return false; } if (exception instanceof SSLException) { return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); if (!(request instanceof HttpEntityEnclosingRequest)) { return true; } return false; } }); // build client client = builder.build(); }
From source file:com.starbucks.apps.HttpUtils.java
private static DefaultHttpClient getHttpClient(HttpInvocationContext context) { DefaultHttpClient client = new DefaultHttpClient(); 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; }/*from w w w . j av a 2 s .co m*/ }); client.addRequestInterceptor(context); client.addResponseInterceptor(context); return client; }
From source file:com.jrodeo.queue.messageset.provider.TestHttpClient5.java
public TestHttpClient5() { super();// ww w .j av a 2 s. c o m HttpParams params = new SyncBasicHttpParams(); params.setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); params.setBooleanParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false); params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false); params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024); params.setIntParameter(HttpConnectionParams.SO_TIMEOUT, 15000); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); this.mgr = new PoolingClientConnectionManager(schemeRegistry); this.httpclient = new DefaultHttpClient(this.mgr, params); this.httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { public boolean retryRequest(final IOException exception, int executionCount, final HttpContext context) { return false; } }); }