Search in sources :

Example 6 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project Signal-Android by WhisperSystems.

the class LegacyMmsConnection method constructHttpClient.

protected CloseableHttpClient constructHttpClient() throws IOException {
    RequestConfig config = RequestConfig.custom().setConnectTimeout(20 * 1000).setConnectionRequestTimeout(20 * 1000).setSocketTimeout(20 * 1000).setMaxRedirects(20).build();
    URL mmsc = new URL(apn.getMmsc());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    if (apn.hasAuthentication()) {
        credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()), new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
    }
    return HttpClients.custom().setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4()).setRedirectStrategy(new LaxRedirectStrategy()).setUserAgent(TextSecurePreferences.getMmsUserAgent(context, USER_AGENT)).setConnectionManager(new BasicHttpClientConnectionManager()).setDefaultRequestConfig(config).setDefaultCredentialsProvider(credsProvider).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) NoConnectionReuseStrategyHC4(org.apache.http.impl.NoConnectionReuseStrategyHC4) AuthScope(org.apache.http.auth.AuthScope) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) URL(java.net.URL) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 7 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project jersey by jersey.

the class ApacheConnector method getUriHttpRequest.

private HttpUriRequest getUriHttpRequest(final ClientRequest clientRequest) {
    final RequestConfig.Builder requestConfigBuilder = RequestConfig.copy(requestConfig);
    final int connectTimeout = clientRequest.resolveProperty(ClientProperties.CONNECT_TIMEOUT, -1);
    final int socketTimeout = clientRequest.resolveProperty(ClientProperties.READ_TIMEOUT, -1);
    if (connectTimeout >= 0) {
        requestConfigBuilder.setConnectTimeout(connectTimeout);
    }
    if (socketTimeout >= 0) {
        requestConfigBuilder.setSocketTimeout(socketTimeout);
    }
    final Boolean redirectsEnabled = clientRequest.resolveProperty(ClientProperties.FOLLOW_REDIRECTS, requestConfig.isRedirectsEnabled());
    requestConfigBuilder.setRedirectsEnabled(redirectsEnabled);
    final Boolean bufferingEnabled = clientRequest.resolveProperty(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.class) == RequestEntityProcessing.BUFFERED;
    final HttpEntity entity = getHttpEntity(clientRequest, bufferingEnabled);
    return RequestBuilder.create(clientRequest.getMethod()).setUri(clientRequest.getUri()).setConfig(requestConfigBuilder.build()).setEntity(entity).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) RequestEntityProcessing(org.glassfish.jersey.client.RequestEntityProcessing)

Example 8 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project jersey by jersey.

the class DisableContentEncodingTest method testEnabledByRequestConfig.

@Test
public void testEnabledByRequestConfig() {
    ClientConfig cc = new ClientConfig(GZipEncoder.class);
    final RequestConfig requestConfig = RequestConfig.custom().setContentCompressionEnabled(true).build();
    cc.property(ApacheClientProperties.REQUEST_CONFIG, requestConfig);
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget r = client.target(getBaseUri());
    String enc = r.request().get().readEntity(String.class);
    assertEquals("gzip,deflate", enc);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 9 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project disconf by knightliao.

the class HttpClientUtil method buildHttpClient.

/**
     * 初始化httpclient对象
     */
private static void buildHttpClient() {
    RequestConfig globalConfig = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();
    CloseableHttpClient httpclient = HttpClients.custom().setKeepAliveStrategy(new HttpClientKeepAliveStrategy()).setDefaultRequestConfig(globalConfig).build();
    HttpClientUtil.httpclient = httpclient;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient)

Example 10 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project DataX by alibaba.

the class RetryUtilTest method testRetryAsync3.

//@Test
@Ignore
public void testRetryAsync3() throws Exception {
    final int TIME_OUT = 30000;
    ThreadPoolExecutor executor = RetryUtil.createThreadPoolExecutor();
    String res = RetryUtil.asyncExecuteWithRetry(new Callable<String>() {

        @Override
        public String call() throws Exception {
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIME_OUT).setConnectTimeout(TIME_OUT).setConnectionRequestTimeout(TIME_OUT).setStaleConnectionCheckEnabled(true).build();
            HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(10).setMaxConnPerRoute(10).setDefaultRequestConfig(requestConfig).build();
            HttpGet httpGet = new HttpGet();
            httpGet.setURI(new URI("http://0.0.0.0:8080/test"));
            httpClient.execute(httpGet);
            return OK;
        }
    }, 3, 1000L, false, 6000L, executor);
    Assert.assertEquals(res, OK);
//        Assert.assertEquals(RetryUtil.EXECUTOR.getActiveCount(), 0);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) URI(java.net.URI) ExpectedException(org.junit.rules.ExpectedException)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)91 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)33 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)31 HttpGet (org.apache.http.client.methods.HttpGet)29 HttpPost (org.apache.http.client.methods.HttpPost)19 Test (org.junit.Test)16 IOException (java.io.IOException)14 StringEntity (org.apache.http.entity.StringEntity)14 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)13 HttpResponse (org.apache.http.HttpResponse)12 WxError (me.chanjar.weixin.common.bean.result.WxError)11 HttpEntity (org.apache.http.HttpEntity)11 URI (java.net.URI)10 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)10 InputStream (java.io.InputStream)9 HttpHost (org.apache.http.HttpHost)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 Configurable (org.apache.http.client.methods.Configurable)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 Header (org.apache.http.Header)6