Search in sources :

Example 16 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project spring-boot by spring-projects.

the class RestTemplateBuilderTests method connectTimeoutCanBeConfiguredOnHttpComponentsRequestFactory.

@Test
public void connectTimeoutCanBeConfiguredOnHttpComponentsRequestFactory() {
    ClientHttpRequestFactory requestFactory = this.builder.requestFactory(HttpComponentsClientHttpRequestFactory.class).setConnectTimeout(1234).build().getRequestFactory();
    assertThat(((RequestConfig) ReflectionTestUtils.getField(requestFactory, "requestConfig")).getConnectTimeout()).isEqualTo(1234);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) SimpleClientHttpRequestFactory(org.springframework.http.client.SimpleClientHttpRequestFactory) Netty4ClientHttpRequestFactory(org.springframework.http.client.Netty4ClientHttpRequestFactory) OkHttp3ClientHttpRequestFactory(org.springframework.http.client.OkHttp3ClientHttpRequestFactory) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) BufferingClientHttpRequestFactory(org.springframework.http.client.BufferingClientHttpRequestFactory) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) Test(org.junit.Test)

Example 17 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.

the class HttpComponentsClientHttpRequestFactoryTests method retrieveRequestConfig.

private RequestConfig retrieveRequestConfig(HttpComponentsClientHttpRequestFactory factory) throws Exception {
    URI uri = new URI(baseUrl + "/status/ok");
    HttpComponentsClientHttpRequest request = (HttpComponentsClientHttpRequest) factory.createRequest(uri, HttpMethod.GET);
    return (RequestConfig) request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) URI(java.net.URI)

Example 18 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.

the class HttpComponentsClientHttpRequestFactoryTests method assertCustomConfig.

@Test
public void assertCustomConfig() throws Exception {
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(httpClient);
    hrf.setConnectTimeout(1234);
    hrf.setConnectionRequestTimeout(4321);
    hrf.setReadTimeout(4567);
    URI uri = new URI(baseUrl + "/status/ok");
    HttpComponentsClientHttpRequest request = (HttpComponentsClientHttpRequest) hrf.createRequest(uri, HttpMethod.GET);
    Object config = request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG);
    assertNotNull("Request config should be set", config);
    assertTrue("Wrong request config type" + config.getClass().getName(), RequestConfig.class.isInstance(config));
    RequestConfig requestConfig = (RequestConfig) config;
    assertEquals("Wrong custom connection timeout", 1234, requestConfig.getConnectTimeout());
    assertEquals("Wrong custom connection request timeout", 4321, requestConfig.getConnectionRequestTimeout());
    assertEquals("Wrong custom socket timeout", 4567, requestConfig.getSocketTimeout());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpClient(org.apache.http.client.HttpClient) URI(java.net.URI) Test(org.junit.Test)

Example 19 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.

the class HttpComponentsHttpInvokerRequestExecutorTests method defaultSettingsOfHttpClientMergedOnExecutorCustomization.

@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws IOException {
    RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
    CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class));
    Configurable configurable = (Configurable) client;
    when(configurable.getConfig()).thenReturn(defaultConfig);
    HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(client);
    HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
    HttpPost httpPost = executor.createHttpPost(config);
    assertSame("Default client configuration is expected", defaultConfig, httpPost.getConfig());
    executor.setConnectionRequestTimeout(4567);
    HttpPost httpPost2 = executor.createHttpPost(config);
    assertNotNull(httpPost2.getConfig());
    assertEquals(4567, httpPost2.getConfig().getConnectionRequestTimeout());
    // Default connection timeout merged
    assertEquals(1234, httpPost2.getConfig().getConnectTimeout());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) Configurable(org.apache.http.client.methods.Configurable) Test(org.junit.Test)

Example 20 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project apn-proxy by apn-proxy.

the class TestProxyWithHttpClient method test.

private void test(String uri, int exceptCode, String exceptHeaderName, String exceptHeaderValue) {
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Consts.UTF_8).build();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(2000);
    cm.setDefaultMaxPerRoute(40);
    cm.setDefaultConnectionConfig(connectionConfig);
    CloseableHttpClient httpClient = HttpClients.custom().setUserAgent("Mozilla/5.0 xx-dev-web-common httpclient/4.x").setConnectionManager(cm).disableContentCompression().disableCookieManagement().build();
    HttpHost proxy = new HttpHost("127.0.0.1", ApnProxyConfig.getConfig().getPort());
    RequestConfig config = RequestConfig.custom().setProxy(proxy).setExpectContinueEnabled(true).setConnectionRequestTimeout(5000).setConnectTimeout(10000).setSocketTimeout(10000).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
    HttpGet request = new HttpGet(uri);
    request.setConfig(config);
    try {
        CloseableHttpResponse httpResponse = httpClient.execute(request);
        Assert.assertEquals(exceptCode, httpResponse.getStatusLine().getStatusCode());
        if (StringUtils.isNotBlank(exceptHeaderName) && StringUtils.isNotBlank(exceptHeaderValue)) {
            Assert.assertEquals(exceptHeaderValue, httpResponse.getFirstHeader(exceptHeaderName).getValue());
        }
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        responseHandler.handleResponse(httpResponse);
        httpResponse.close();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) HttpHost(org.apache.http.HttpHost) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler) IOException(java.io.IOException) ConnectionConfig(org.apache.http.config.ConnectionConfig) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)82 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)28 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)27 HttpGet (org.apache.http.client.methods.HttpGet)26 HttpPost (org.apache.http.client.methods.HttpPost)18 Test (org.junit.Test)15 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)13 StringEntity (org.apache.http.entity.StringEntity)13 IOException (java.io.IOException)12 WxError (me.chanjar.weixin.common.bean.result.WxError)11 URI (java.net.URI)9 HttpEntity (org.apache.http.HttpEntity)9 HttpResponse (org.apache.http.HttpResponse)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 InputStream (java.io.InputStream)8 HttpHost (org.apache.http.HttpHost)8 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)8 Configurable (org.apache.http.client.methods.Configurable)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 HttpClient (org.apache.http.client.HttpClient)6