org.apache.hc.client5.http.config.RequestConfig Java Examples

The following examples show how to use org.apache.hc.client5.http.config.RequestConfig. 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: HttpUtils.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
public static String retrieveHttpAsString(String url) throws HttpException, IOException {
    RequestConfig requestConfig = RequestConfig.custom()
            .setResponseTimeout(5000, TimeUnit.MILLISECONDS)
            .setConnectTimeout(5000, TimeUnit.MILLISECONDS)
            .setConnectionRequestTimeout(5000, TimeUnit.MILLISECONDS)
            .setCookieSpec(StandardCookieSpec.IGNORE)
            .build();
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultRequestConfig(requestConfig)
            .setUserAgent(_userAgent)
            .build();
    HttpGet httpGet = new HttpGet(url);
    httpGet.setConfig(requestConfig);
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpGet);
        final int statusCode = response.getCode();
        if (statusCode != HttpStatus.SC_OK) {
            throw new HttpException("Error " + statusCode + " for URL " + url);
        }
        HttpEntity entity = response.getEntity();
        String data = EntityUtils.toString(entity);
        EntityUtils.consume(entity);
        return data;
    } catch (IOException e) {
        throw new IOException("Error for URL " + url, e);
    } finally {
        if (response != null) {
            response.close();
        }
        httpclient.close();
    }
}
 
Example #2
Source File: HttpClient.java    From webdrivermanager with Apache License 2.0 5 votes vote down vote up
public HttpGet createHttpGet(URL url) {
    HttpGet httpGet = new HttpGet(url.toString());
    httpGet.addHeader("user-agent", "Apache-HttpClient/5.0");
    httpGet.addHeader("accept-encoding", "gzip, deflate, br");
    httpGet.addHeader("cache-control", "max-age=0");

    RequestConfig requestConfig = custom().setCookieSpec(STRICT)
            .setConnectTimeout(config.getTimeout(), TimeUnit.SECONDS)
            .build();
    httpGet.setConfig(requestConfig);
    return httpGet;
}
 
Example #3
Source File: HttpUtils.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
public static String retrieveHttpAsString(String url) throws HttpException, IOException {
    RequestConfig requestConfig = RequestConfig.custom()
            .setResponseTimeout(5000, TimeUnit.MILLISECONDS)
            .setConnectTimeout(5000, TimeUnit.MILLISECONDS)
            .setConnectionRequestTimeout(5000, TimeUnit.MILLISECONDS)
            .setCookieSpec(StandardCookieSpec.IGNORE)
            .build();
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultRequestConfig(requestConfig)
            .setUserAgent(_userAgent)
            .build();
    HttpGet httpGet = new HttpGet(url);
    httpGet.setConfig(requestConfig);
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpGet);
        final int statusCode = response.getCode();
        if (statusCode != HttpStatus.SC_OK) {
            throw new HttpException("Error " + statusCode + " for URL " + url);
        }
        HttpEntity entity = response.getEntity();
        String data = EntityUtils.toString(entity);
        EntityUtils.consume(entity);
        return data;
    } catch (IOException e) {
        throw new IOException("Error for URL " + url, e);
    } finally {
        if (response != null) {
            response.close();
        }
        httpclient.close();
    }
}
 
Example #4
Source File: ApacheHttp5Client.java    From feign with Apache License 2.0 5 votes vote down vote up
protected HttpClientContext configureTimeouts(Request.Options options) {
  final HttpClientContext context = new HttpClientContext();
  // per request timeouts
  final RequestConfig requestConfig =
      (client instanceof Configurable
          ? RequestConfig.copy(((Configurable) client).getConfig())
          : RequestConfig.custom())
              .setConnectTimeout(options.connectTimeout(), options.connectTimeoutUnit())
              .setResponseTimeout(options.readTimeout(), options.readTimeoutUnit())
              .build();
  context.setRequestConfig(requestConfig);
  return context;
}
 
Example #5
Source File: AsyncApacheHttp5Client.java    From feign with Apache License 2.0 5 votes vote down vote up
protected HttpClientContext configureTimeouts(Request.Options options,
                                              HttpClientContext context) {
  // per request timeouts
  final RequestConfig requestConfig =
      (client instanceof Configurable
          ? RequestConfig.copy(((Configurable) client).getConfig())
          : RequestConfig.custom())
              .setConnectTimeout(options.connectTimeout(), options.connectTimeoutUnit())
              .setResponseTimeout(options.readTimeout(), options.readTimeoutUnit())
              .build();
  context.setRequestConfig(requestConfig);
  return context;
}
 
Example #6
Source File: SpotifyHttpManager.java    From spotify-web-api-java with MIT License 4 votes vote down vote up
/**
 * Construct a new SpotifyHttpManager instance.
 *
 * @param builder The builder.
 */
public SpotifyHttpManager(Builder builder) {
  this.proxy = builder.proxy;
  this.proxyCredentials = builder.proxyCredentials;
  this.cacheMaxEntries = builder.cacheMaxEntries;
  this.cacheMaxObjectSize = builder.cacheMaxObjectSize;
  this.connectionRequestTimeout = builder.connectionRequestTimeout;
  this.connectTimeout = builder.connectTimeout;
  this.socketTimeout = builder.socketTimeout;

  CacheConfig cacheConfig = CacheConfig.custom()
    .setMaxCacheEntries(cacheMaxEntries != null ? cacheMaxEntries : DEFAULT_CACHE_MAX_ENTRIES)
    .setMaxObjectSize(cacheMaxObjectSize != null ? cacheMaxObjectSize : DEFAULT_CACHE_MAX_OBJECT_SIZE)
    .setSharedCache(false)
    .build();

  BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();

  if (proxy != null) {
    credentialsProvider.setCredentials(
      new AuthScope(null, proxy.getHostName(), proxy.getPort(), null, proxy.getSchemeName()),
      proxyCredentials
    );
  }

  RequestConfig requestConfig = RequestConfig
    .custom()
    .setCookieSpec(StandardCookieSpec.STRICT)
    .setProxy(proxy)
    .setConnectionRequestTimeout(builder.connectionRequestTimeout != null
      ? Timeout.ofMilliseconds(builder.connectionRequestTimeout)
      : RequestConfig.DEFAULT.getConnectionRequestTimeout())
    .setConnectTimeout(builder.connectTimeout != null
      ? Timeout.ofMilliseconds(builder.connectTimeout)
      : RequestConfig.DEFAULT.getConnectTimeout())
    .setResponseTimeout(builder.socketTimeout != null
      ? Timeout.ofMilliseconds(builder.socketTimeout)
      : RequestConfig.DEFAULT.getResponseTimeout())
    .build();

  this.httpClient = HttpClients
    .custom()
    .setDefaultCredentialsProvider(credentialsProvider)
    .setDefaultRequestConfig(requestConfig)
    .disableContentCompression()
    .build();

  this.httpClientCaching = CachingHttpClients
    .custom()
    .setCacheConfig(cacheConfig)
    .setDefaultCredentialsProvider(credentialsProvider)
    .setDefaultRequestConfig(requestConfig)
    .disableContentCompression()
    .build();
}
 
Example #7
Source File: Http5FileProvider.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
private RequestConfig createDefaultRequestConfig(final Http5FileSystemConfigBuilder builder,
        final FileSystemOptions fileSystemOptions) {
    return RequestConfig.custom()
            .setConnectTimeout(builder.getConnectionTimeout(fileSystemOptions), TimeUnit.MILLISECONDS)
            .build();
}