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() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
@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
/**
 * 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 Project: AndroidRobot   File: HttpClientUtil.java    License: Apache License 2.0 6 votes vote down vote up
/** 
 * 获取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 4
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 5
Source Project: micro-integrator   File: SOAPClient.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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
private void configureRetryHandler(DefaultHttpClient httpClient) {
    httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            return false;
        }
    });
}
 
Example 7
private void configureRetryHandler(DefaultHttpClient httpClient) {
    httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            return false;
        }
    });
}
 
Example 8
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 9
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 10
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 11
Source Project: product-ei   File: SOAPClient.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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 12
private void configureRetryHandler(DefaultHttpClient httpClient) {
    httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            return false;
        }
    });
}
 
Example 13
private void configureRetryHandler(DefaultHttpClient httpClient) {
    httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            return false;
        }
    });
}
 
Example 14
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 15
/**
 * 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 16
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 17
Source Project: freeacs   File: Kick.java    License: MIT License 4 votes vote down vote up
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 18
Source Project: YiBo   File: HttpRequestHelper.java    License: Apache License 2.0 4 votes vote down vote up
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
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 20
@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;
}