Example usage for org.apache.http.impl.client DefaultHttpRequestRetryHandler DefaultHttpRequestRetryHandler

List of usage examples for org.apache.http.impl.client DefaultHttpRequestRetryHandler DefaultHttpRequestRetryHandler

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpRequestRetryHandler DefaultHttpRequestRetryHandler.

Prototype

@SuppressWarnings("unchecked")
public DefaultHttpRequestRetryHandler(final int retryCount, final boolean requestSentRetryEnabled) 

Source Link

Document

Create the request retry handler using the following list of non-retriable IOException classes:
  • InterruptedIOException
  • UnknownHostException
  • ConnectException
  • SSLException

Usage

From source file:ninja.siden.internal.Testing.java

static CloseableHttpClient client() {
    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(200).build());
    builder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
    return builder.build();
}

From source file:Main.java

public static DefaultHttpClient getDefaultHttpClient() {
    SchemeRegistry schemeRegistry = new SchemeRegistry();

    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    HttpParams params = new BasicHttpParams();
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
    DefaultHttpClient defaultHttpClient = new DefaultHttpClient(cm, params);

    HttpConnectionParams.setSoTimeout(defaultHttpClient.getParams(), SOCKET_TIME_OUT);
    HttpConnectionParams.setConnectionTimeout(defaultHttpClient.getParams(), CONNECTION_TIME_OUT);
    defaultHttpClient.getParams().setIntParameter(ClientPNames.MAX_REDIRECTS, 10);
    HttpClientParams.setCookiePolicy(defaultHttpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);
    HttpProtocolParams.setUserAgent(defaultHttpClient.getParams(),
            "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110628 Ubuntu/10.04 (lucid) Firefox/3.6.18");
    defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true));
    return defaultHttpClient;
}

From source file:org.wso2.security.tools.product.manager.handler.HttpRequestHandler.java

/**
 * Send HTTP GET request//from  www  . ja va  2  s  . co  m
 *
 * @param request Requested URI
 * @return HTTPResponse after executing the command
 */
public static HttpResponse sendGetRequest(URI request) {
    try {
        HttpClientBuilder clientBuilder = HttpClients.custom();
        HttpClient httpClient = clientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3, false))
                .build();
        HttpGet httpGetRequest = new HttpGet(request);
        return httpClient.execute(httpGetRequest);
    } catch (IOException e) {
        LOGGER.error("Error occurred while sending GET request to " + request.getPath(), e);
    }
    return null;
}

From source file:ph.sakay.gateway.APIClient.java

private HttpClient getHttpClient() {
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    int timeoutConnection = 5000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 300000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    DefaultHttpClient client = new DefaultHttpClient(httpParameters);
    client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(1, false));
    return client;
}

From source file:org.red5.server.util.HttpConnectionUtil.java

/**
 * Returns a client with all our selected properties / params.
 * //from  w  ww  .  j ava  2s . c om
 * @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;
}

From source file:org.zenoss.app.consumer.metric.impl.MetricServicePoster.java

private static final DefaultHttpClient newHttpClient() {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(Integer.MAX_VALUE, true));
    return httpClient;
}

From source file:com.andrestrequest.http.HttpClientBuilder.java

/**
 * @param retryCount//  ww w.j  a  v a2  s  . c  om
 *            number of retries to be attempted by the http client
 */
public void setRetryCount(int retryCount) {
    retryHandler = new DefaultHttpRequestRetryHandler(retryCount, false);
}

From source file:org.wso2.security.tools.product.manager.handler.HttpRequestHandler.java

/**
 * Send HTTP POST request/*from  ww w.  ja  va  2 s.  c o  m*/
 *
 * @param requestURI Requested URI
 * @return HTTPResponse after executing the command
 */
public static HttpResponse sendPostRequest(String requestURI, ArrayList<NameValuePair> parameters) {
    try {
        HttpClientBuilder clientBuilder = HttpClients.custom();
        HttpClient httpClient = clientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3, false))
                .build();
        HttpPost httpPostRequest = new HttpPost(requestURI);

        for (NameValuePair parameter : parameters) {
            urlParameters.add(new BasicNameValuePair(parameter.getName(), parameter.getValue()));
        }

        httpPostRequest.setEntity(new UrlEncodedFormEntity(urlParameters));
        return httpClient.execute(httpPostRequest);
    } catch (IOException e) {
        LOGGER.error("Error occurred while sending POST request to " + requestURI, e);
    }
    return null;
}

From source file:com.futureplatforms.kirin.internal.attic.IOUtils.java

public static HttpClient newHttpClient() {
    int timeout = 3 * 60 * 1000;
    DefaultHttpClient httpClient = new DefaultHttpClient();
    ClientConnectionManager mgr = httpClient.getConnectionManager();
    HttpParams params = httpClient.getParams();
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry());
    httpClient = new DefaultHttpClient(cm, params);

    // how long are we prepared to wait to establish a connection?
    HttpConnectionParams.setConnectionTimeout(params, timeout);

    // how long should the socket wait for data?
    HttpConnectionParams.setSoTimeout(params, timeout);

    httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, false) {

        @Override// www  .j  a va  2s .co m
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            return super.retryRequest(exception, executionCount, context);
        }

        @Override
        public boolean isRequestSentRetryEnabled() {
            return false;
        }
    });

    httpClient.addRequestInterceptor(new HttpRequestInterceptor() {

        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }
        }

    });

    httpClient.addResponseInterceptor(new HttpResponseInterceptor() {

        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            response.removeHeaders("Set-Cookie");

            HttpEntity entity = response.getEntity();
            Header contentEncodingHeader = entity.getContentEncoding();
            if (contentEncodingHeader != null) {
                HeaderElement[] codecs = contentEncodingHeader.getElements();
                for (int i = 0; i < codecs.length; i++) {
                    if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }

        }

    });
    return httpClient;
}

From source file:com.amazonaws.http.ApacheHttpClient.java

public ApacheHttpClient(ClientConfiguration config) {
    HttpClientFactory httpClientFactory = new HttpClientFactory();
    httpClient = httpClientFactory.createHttpClient(config);
    // disable retry
    ((AbstractHttpClient) httpClient).setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));

    SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();
    Scheme https = schemeRegistry.getScheme("https");
    ((SSLSocketFactory) https.getSocketFactory())
            .setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
}