Search in sources :

Example 71 with RequestConfig

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());
}
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 72 with RequestConfig

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();
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) Gson(com.google.gson.Gson) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 73 with RequestConfig

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();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) NameValuePair(org.apache.http.NameValuePair) RequestBuilder(org.apache.http.client.methods.RequestBuilder) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder) StringEntity(org.apache.http.entity.StringEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 74 with RequestConfig

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;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CookieSpecProvider(org.apache.http.cookie.CookieSpecProvider) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder)

Example 75 with RequestConfig

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();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

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