Search in sources :

Example 21 with HttpClientBuilder

use of org.apache.http.impl.client.HttpClientBuilder in project intellij-community by JetBrains.

the class GithubConnectionBuilder method createClient.

@NotNull
public CloseableHttpClient createClient() {
    HttpClientBuilder builder = HttpClients.custom();
    builder.setDefaultRequestConfig(createRequestConfig()).setDefaultConnectionConfig(createConnectionConfig()).setDefaultHeaders(createHeaders()).setSslcontext(CertificateManager.getInstance().getSslContext());
    setupCredentialsProvider(builder);
    return builder.build();
}
Also used : HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with HttpClientBuilder

use of org.apache.http.impl.client.HttpClientBuilder in project openstack4j by ContainX.

the class HttpClientFactory method buildClient.

private CloseableHttpClient buildClient(Config config) {
    HttpClientBuilder cb = HttpClientBuilder.create().setUserAgent(USER_AGENT);
    if (config.getProxy() != null) {
        try {
            URL url = new URL(config.getProxy().getHost());
            HttpHost proxy = new HttpHost(url.getHost(), config.getProxy().getPort(), url.getProtocol());
            cb.setProxy(proxy);
        } catch (MalformedURLException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    if (config.isIgnoreSSLVerification()) {
        cb.setSslcontext(UntrustedSSL.getSSLContext());
        cb.setHostnameVerifier(new AllowAllHostnameVerifier());
    }
    if (config.getSslContext() != null)
        cb.setSslcontext(config.getSslContext());
    if (config.getMaxConnections() > 0) {
        cb.setMaxConnTotal(config.getMaxConnections());
    }
    if (config.getMaxConnectionsPerRoute() > 0) {
        cb.setMaxConnPerRoute(config.getMaxConnectionsPerRoute());
    }
    RequestConfig.Builder rcb = RequestConfig.custom();
    if (config.getConnectTimeout() > 0)
        rcb.setConnectTimeout(config.getConnectTimeout());
    if (config.getReadTimeout() > 0)
        rcb.setSocketTimeout(config.getReadTimeout());
    if (INTERCEPTOR != null) {
        INTERCEPTOR.onClientCreate(cb, rcb, config);
    }
    return cb.setDefaultRequestConfig(rcb.build()).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) MalformedURLException(java.net.MalformedURLException) HttpHost(org.apache.http.HttpHost) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URL(java.net.URL)

Example 23 with HttpClientBuilder

use of org.apache.http.impl.client.HttpClientBuilder in project pact-jvm by DiUS.

the class InsecureHttpsRequest method setupInsecureSSL.

private void setupInsecureSSL() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
    HttpClientBuilder b = HttpClientBuilder.create();
    // setup a Trust Strategy that allows all certificates.
    //
    TrustStrategy trustStrategy = (chain, authType) -> true;
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build();
    b.setSSLContext(sslContext);
    // don't check Hostnames, either.
    //      -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
    HostnameVerifier hostnameVerifier = new NoopHostnameVerifier();
    // here's the special part:
    //      -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
    //      -- and create a Registry, to register it.
    //
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
    // now, we create connection-manager using our Registry.
    //      -- allows multi-threaded use
    PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    b.setConnectionManager(connMgr);
    // finally, build the HttpClient;
    //      -- done!
    this.httpclient = b.build();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) TrustStrategy(org.apache.http.ssl.TrustStrategy) SSLContext(javax.net.ssl.SSLContext) RegistryBuilder(org.apache.http.config.RegistryBuilder) HttpOptions(org.apache.http.client.methods.HttpOptions) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) HttpPut(org.apache.http.client.methods.HttpPut) Registry(org.apache.http.config.Registry) HttpGet(org.apache.http.client.methods.HttpGet) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URI(java.net.URI) HostnameVerifier(javax.net.ssl.HostnameVerifier) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustStrategy(org.apache.http.ssl.TrustStrategy) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLContext(javax.net.ssl.SSLContext) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 24 with HttpClientBuilder

use of org.apache.http.impl.client.HttpClientBuilder in project SmartApplianceEnabler by camueller.

the class HttpTransactionExecutor method sendHttpRequest.

/**
     * Send a HTTP request whose response has to be closed by the caller!
     * @param url
     * @return
     */
protected CloseableHttpResponse sendHttpRequest(String url, String data, ContentType contentType, String username, String password) {
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    if (username != null && password != null) {
        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        provider.setCredentials(AuthScope.ANY, credentials);
        httpClientBuilder.setDefaultCredentialsProvider(provider);
    }
    CloseableHttpClient client = httpClientBuilder.build();
    logger.debug("Sending HTTP request");
    logger.debug("url=" + url);
    logger.debug("data=" + data);
    logger.debug("contentType=" + contentType);
    logger.debug("username=" + username);
    logger.debug("password=" + password);
    try {
        HttpRequestBase request = null;
        if (data != null) {
            request = new HttpPost(url);
            ((HttpPost) request).setEntity(new StringEntity(data, contentType));
        } else {
            request = new HttpGet(url);
        }
        CloseableHttpResponse response = client.execute(request);
        int responseCode = response.getStatusLine().getStatusCode();
        logger.debug("Response code is " + responseCode);
        return response;
    } catch (IOException e) {
        logger.error("Error executing HTTP request.", e);
        return null;
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 25 with HttpClientBuilder

use of org.apache.http.impl.client.HttpClientBuilder in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method setWxMpConfigStorage.

public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) {
    this.wxMpConfigStorage = wxConfigProvider;
    String http_proxy_host = wxMpConfigStorage.getHttp_proxy_host();
    int http_proxy_port = wxMpConfigStorage.getHttp_proxy_port();
    String http_proxy_username = wxMpConfigStorage.getHttp_proxy_username();
    String http_proxy_password = wxMpConfigStorage.getHttp_proxy_password();
    final HttpClientBuilder builder = HttpClients.custom();
    if (StringUtils.isNotBlank(http_proxy_host)) {
        // 使用代理服务器
        if (StringUtils.isNotBlank(http_proxy_username)) {
            // 需要用户认证的代理服务器
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(http_proxy_host, http_proxy_port), new UsernamePasswordCredentials(http_proxy_username, http_proxy_password));
            builder.setDefaultCredentialsProvider(credsProvider);
        } else {
        // 无需用户认证的代理服务器
        }
        httpProxy = new HttpHost(http_proxy_host, http_proxy_port);
    }
    if (wxConfigProvider.getSSLContext() != null) {
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(wxConfigProvider.getSSLContext(), new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        builder.setSSLSocketFactory(sslsf);
    }
    httpClient = builder.build();
}
Also used : 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) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)42 RequestConfig (org.apache.http.client.config.RequestConfig)17 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)12 HttpHost (org.apache.http.HttpHost)10 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)10 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)10 SSLContext (javax.net.ssl.SSLContext)9 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)9 CredentialsProvider (org.apache.http.client.CredentialsProvider)9 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)9 HttpClient (org.apache.http.client.HttpClient)8 IOException (java.io.IOException)7 HttpResponse (org.apache.http.HttpResponse)7 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)7 AuthScope (org.apache.http.auth.AuthScope)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 HttpGet (org.apache.http.client.methods.HttpGet)5 HttpPost (org.apache.http.client.methods.HttpPost)5 URI (java.net.URI)4 HttpEntity (org.apache.http.HttpEntity)4