use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.
the class HttpComponentsHttpInvokerRequestExecutorTests method mergeBasedOnCurrentHttpClient.
@Test
public void mergeBasedOnCurrentHttpClient() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom().setSocketTimeout(1234).build();
final CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor() {
@Override
public HttpClient getHttpClient() {
return client;
}
};
executor.setReadTimeout(5000);
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
RequestConfig requestConfig = httpPost.getConfig();
assertEquals(-1, requestConfig.getConnectTimeout());
assertEquals(-1, requestConfig.getConnectionRequestTimeout());
assertEquals(5000, requestConfig.getSocketTimeout());
// Update the Http client so that it returns an updated config
RequestConfig updatedDefaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
when(configurable.getConfig()).thenReturn(updatedDefaultConfig);
executor.setReadTimeout(7000);
HttpPost httpPost2 = executor.createHttpPost(config);
RequestConfig requestConfig2 = httpPost2.getConfig();
assertEquals(1234, requestConfig2.getConnectTimeout());
assertEquals(-1, requestConfig2.getConnectionRequestTimeout());
assertEquals(7000, requestConfig2.getSocketTimeout());
}
use of org.apache.http.client.config.RequestConfig in project ECTranslation by Skykai521.
the class RequestRunnable method run.
public void run() {
try {
URI uri = createTranslationURI(mQuery);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).build();
HttpGet httpGet = new HttpGet(uri);
httpGet.setConfig(requestConfig);
HttpClient client = HttpClients.createDefault();
HttpResponse response = client.execute(httpGet);
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity resEntity = response.getEntity();
String json = EntityUtils.toString(resEntity, "UTF-8");
Gson gson = new Gson();
Translation translation = gson.fromJson(json, Translation.class);
//show result
showPopupBalloon(translation.toString());
Logger.info(translation.toString());
} else {
showPopupBalloon(response.getStatusLine().getReasonPhrase());
}
} catch (IOException e) {
showPopupBalloon(e.getMessage());
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
use of org.apache.http.client.config.RequestConfig in project feign by OpenFeign.
the class ApacheHttpClient method toHttpUriRequest.
HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
RequestBuilder requestBuilder = RequestBuilder.create(request.method());
//per request timeouts
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(options.connectTimeoutMillis()).setSocketTimeout(options.readTimeoutMillis()).build();
requestBuilder.setConfig(requestConfig);
URI uri = new URIBuilder(request.url()).build();
requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getRawPath());
//request query params
List<NameValuePair> queryParams = URLEncodedUtils.parse(uri, requestBuilder.getCharset().name());
for (NameValuePair queryParam : queryParams) {
requestBuilder.addParameter(queryParam);
}
//request headers
boolean hasAcceptHeader = false;
for (Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) {
String headerName = headerEntry.getKey();
if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) {
hasAcceptHeader = true;
}
if (headerName.equalsIgnoreCase(Util.CONTENT_LENGTH)) {
// doesn't like us to set it as well.
continue;
}
for (String headerValue : headerEntry.getValue()) {
requestBuilder.addHeader(headerName, headerValue);
}
}
//some servers choke on the default accept string, so we'll set it to anything
if (!hasAcceptHeader) {
requestBuilder.addHeader(ACCEPT_HEADER_NAME, "*/*");
}
//request body
if (request.body() != null) {
HttpEntity entity = null;
if (request.charset() != null) {
ContentType contentType = getContentType(request);
String content = new String(request.body(), request.charset());
entity = new StringEntity(content, contentType);
} else {
entity = new ByteArrayEntity(request.body());
}
requestBuilder.setEntity(entity);
}
return requestBuilder.build();
}
use of org.apache.http.client.config.RequestConfig in project wildfly by wildfly.
the class TestHttpClientUtils method promiscuousCookieHttpClientBuilder.
/**
* Same as {@link TestHttpClientUtils#promiscuousCookieHttpClient()} but instead returns a builder that can be further configured.
*
* @return {@link HttpClientBuilder} of the http client that gives free cookies to everybody
* @see TestHttpClientUtils#promiscuousCookieHttpClient()
*/
public static HttpClientBuilder promiscuousCookieHttpClientBuilder() {
HttpClientBuilder builder = HttpClients.custom();
RegistryBuilder<CookieSpecProvider> registryBuilder = CookieSpecRegistries.createDefaultBuilder();
Registry<CookieSpecProvider> promiscuousCookieSpecRegistry = registryBuilder.register("promiscuous", new PromiscuousCookieSpecProvider()).build();
builder.setDefaultCookieSpecRegistry(promiscuousCookieSpecRegistry);
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec("promiscuous").build();
builder.setDefaultRequestConfig(requestConfig);
builder.setDefaultCookieStore(new PromiscuousCookieStore());
return builder;
}
use of org.apache.http.client.config.RequestConfig in project Asqatasun by Asqatasun.
the class HttpRequestHandler method getHttpClient.
private CloseableHttpClient getHttpClient(String url) {
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectionTimeout).build();
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setDefaultRequestConfig(requestConfig);
httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager());
httpClientBuilder.setUserAgent(ASQATASUN_USER_AGENT);
if (isProxySet(url)) {
LOGGER.debug(("Set proxy with " + proxyHost + " and " + proxyPort));
httpClientBuilder.setProxy(new HttpHost(proxyHost, Integer.valueOf(proxyPort)));
if (isProxyCredentialSet()) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost, Integer.valueOf(proxyPort)), new UsernamePasswordCredentials(proxyUser, proxyPassword));
httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
LOGGER.debug(("Set proxy credentials " + proxyHost + " and " + proxyPort + " and " + proxyUser + " and " + proxyPassword));
}
}
return httpClientBuilder.build();
}
Aggregations