Java Code Examples for org.apache.http.impl.client.HttpClientBuilder#setDefaultSocketConfig()
The following examples show how to use
org.apache.http.impl.client.HttpClientBuilder#setDefaultSocketConfig() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SyncHttpClientGenerator.java From cetty with Apache License 2.0 | 6 votes |
@Override protected CloseableHttpClient build(Payload payload) { HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager); if (payload.getUserAgent() != null) { httpClientBuilder.setUserAgent(payload.getUserAgent()); } else { httpClientBuilder.setUserAgent(""); } httpClientBuilder.setConnectionManagerShared(true); httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy()); SocketConfig.Builder socketConfigBuilder = SocketConfig.custom(); socketConfigBuilder.setSoKeepAlive(true).setTcpNoDelay(true); socketConfigBuilder.setSoTimeout(payload.getSocketTimeout()); SocketConfig socketConfig = socketConfigBuilder.build(); httpClientBuilder.setDefaultSocketConfig(socketConfig); poolingHttpClientConnectionManager.setDefaultSocketConfig(socketConfig); httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(payload.getRetryTimes(), true)); reduceCookie(httpClientBuilder, payload); return httpClientBuilder.build(); }
Example 2
Source File: AbstractUnitTest.java From elasticsearch-shield-kerberos-realm with Apache License 2.0 | 6 votes |
protected final CloseableHttpClient getHttpClient(final boolean useSpnego) throws Exception { final CredentialsProvider credsProvider = new BasicCredentialsProvider(); final HttpClientBuilder hcb = HttpClients.custom(); if (useSpnego) { //SPNEGO/Kerberos setup log.debug("SPNEGO activated"); final AuthSchemeProvider nsf = new SPNegoSchemeFactory(true);// new NegotiateSchemeProvider(); final Credentials jaasCreds = new JaasCredentials(); credsProvider.setCredentials(new AuthScope(null, -1, null, AuthSchemes.SPNEGO), jaasCreds); credsProvider.setCredentials(new AuthScope(null, -1, null, AuthSchemes.NTLM), new NTCredentials("Guest", "Guest", "Guest", "Guest")); final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider> create() .register(AuthSchemes.SPNEGO, nsf).register(AuthSchemes.NTLM, new NTLMSchemeFactory()).build(); hcb.setDefaultAuthSchemeRegistry(authSchemeRegistry); } hcb.setDefaultCredentialsProvider(credsProvider); hcb.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(10 * 1000).build()); final CloseableHttpClient httpClient = hcb.build(); return httpClient; }
Example 3
Source File: HttpWorkflowStepPlugin.java From rundeck-http-plugin with ISC License | 6 votes |
protected HttpClient getHttpClient(Map<String, Object> options) throws GeneralSecurityException { SocketConfig socketConfig = SocketConfig.custom() .setSoKeepAlive(true).build(); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setDefaultSocketConfig(socketConfig); httpClientBuilder.disableAuthCaching(); httpClientBuilder.disableAutomaticRetries(); if(options.containsKey("sslVerify") && !Boolean.parseBoolean(options.get("sslVerify").toString())) { log.debug("Disabling all SSL certificate verification."); SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { return true; } }); httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier()); httpClientBuilder.setSSLContext(sslContextBuilder.build()); } return httpClientBuilder.build(); }
Example 4
Source File: HttpClientGenerator.java From plumemo with Apache License 2.0 | 5 votes |
private CloseableHttpClient generateClient(Site site) { HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setConnectionManager(connectionManager); if (site.getUserAgent() != null) { httpClientBuilder.setUserAgent(site.getUserAgent()); } else { httpClientBuilder.setUserAgent(""); } if (site.isUseGzip()) { httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() { @Override public void process( HttpRequest request, HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } } }); } //解决post/redirect/post 302跳转问题 httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy()); SocketConfig.Builder socketConfigBuilder = SocketConfig.custom(); socketConfigBuilder.setSoKeepAlive(true).setTcpNoDelay(true); socketConfigBuilder.setSoTimeout(site.getTimeOut()); SocketConfig socketConfig = socketConfigBuilder.build(); httpClientBuilder.setDefaultSocketConfig(socketConfig); connectionManager.setDefaultSocketConfig(socketConfig); httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(site.getRetryTimes(), true)); generateCookie(httpClientBuilder, site); return httpClientBuilder.build(); }
Example 5
Source File: HttpWebConnection.java From htmlunit with Apache License 2.0 | 5 votes |
private void configureTimeout(final HttpClientBuilder builder, final int timeout) { final InetAddress localAddress = webClient_.getOptions().getLocalAddress(); final RequestConfig.Builder requestBuilder = createRequestConfigBuilder(timeout, localAddress); builder.setDefaultRequestConfig(requestBuilder.build()); builder.setDefaultSocketConfig(createSocketConfigBuilder(timeout).build()); getHttpContext().removeAttribute(HttpClientContext.REQUEST_CONFIG); usedOptions_.setTimeout(timeout); }
Example 6
Source File: HttpClientUtils.java From rpc-benchmark with Apache License 2.0 | 5 votes |
public static CloseableHttpClient createHttpClient(int concurrency) { HttpClientBuilder builder = HttpClientBuilder.create(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setDefaultMaxPerRoute(concurrency); connManager.setMaxTotal(concurrency); RequestConfig requestConfig = RequestConfig.custom()// .setAuthenticationEnabled(true)// .setSocketTimeout(SOCKET_TIMEOUT)// .setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)// .setConnectTimeout(CONNECT_TIMEOUT)// .setRedirectsEnabled(true)// .setRelativeRedirectsAllowed(true)// .setMaxRedirects(15)// .build(); SocketConfig socketConfig = SocketConfig.custom()// .setSoKeepAlive(true)// .setSoReuseAddress(true)// .build(); builder.setConnectionManager(connManager); builder.setDefaultSocketConfig(socketConfig); builder.setDefaultRequestConfig(requestConfig); return builder.build(); }
Example 7
Source File: HttpClientGenerator.java From zongtui-webcrawler with GNU General Public License v2.0 | 5 votes |
private CloseableHttpClient generateClient(Site site) { HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(connectionManager); if (site != null && site.getUserAgent() != null) { httpClientBuilder.setUserAgent(site.getUserAgent()); } else { httpClientBuilder.setUserAgent(""); } if (site == null || site.isUseGzip()) { httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() { public void process( final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } } }); } SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build(); httpClientBuilder.setDefaultSocketConfig(socketConfig); if (site != null) { httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(site.getRetryTimes(), true)); } generateCookie(httpClientBuilder, site); return httpClientBuilder.build(); }
Example 8
Source File: HttpClientGenerator.java From webmagic with Apache License 2.0 | 5 votes |
private CloseableHttpClient generateClient(Site site) { HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setConnectionManager(connectionManager); if (site.getUserAgent() != null) { httpClientBuilder.setUserAgent(site.getUserAgent()); } else { httpClientBuilder.setUserAgent(""); } if (site.isUseGzip()) { httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() { public void process( final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } } }); } //解决post/redirect/post 302跳转问题 httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy()); SocketConfig.Builder socketConfigBuilder = SocketConfig.custom(); socketConfigBuilder.setSoKeepAlive(true).setTcpNoDelay(true); socketConfigBuilder.setSoTimeout(site.getTimeOut()); SocketConfig socketConfig = socketConfigBuilder.build(); httpClientBuilder.setDefaultSocketConfig(socketConfig); connectionManager.setDefaultSocketConfig(socketConfig); httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(site.getRetryTimes(), true)); generateCookie(httpClientBuilder, site); return httpClientBuilder.build(); }
Example 9
Source File: HttpClientFactory.java From vividus with Apache License 2.0 | 4 votes |
@Override public IHttpClient buildHttpClient(HttpClientConfig config) { HttpClientBuilder builder = HttpClientBuilder.create(); builder.setDefaultHeaders(config.createHeaders()); if (config.hasCookieStore()) { builder.setDefaultCookieStore(config.getCookieStore()); } if (config.hasCredentials()) { AuthScope authScope = config.hasAuthScope() ? config.getAuthScope() : ClientBuilderUtils.DEFAULT_AUTH_SCOPE; CredentialsProvider credProvider = ClientBuilderUtils.createCredentialsProvider(authScope, config.getCredentials()); builder.setDefaultCredentialsProvider(credProvider); } sslContextFactory.getSslContext(SSLConnectionSocketFactory.SSL, !config.isSslCertificateCheckEnabled()) .ifPresent(builder::setSSLContext); if (!config.isSslHostnameVerificationEnabled()) { builder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); } builder.setConnectionManager(config.getConnectionManager()); builder.setMaxConnTotal(config.getMaxTotalConnections()); builder.setMaxConnPerRoute(config.getMaxConnectionsPerRoute()); builder.addInterceptorLast(config.getLastRequestInterceptor()); builder.addInterceptorLast(config.getLastResponseInterceptor()); builder.setRedirectStrategy(config.getRedirectStrategy()); builder.setRetryHandler(config.getHttpRequestRetryHandler()); Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectionRequestTimeout(config.getConnectionRequestTimeout()); requestConfigBuilder.setConnectTimeout(config.getConnectTimeout()); requestConfigBuilder.setCircularRedirectsAllowed(config.isCircularRedirectsAllowed()); requestConfigBuilder.setSocketTimeout(config.getSocketTimeout()); Optional.ofNullable(config.getCookieSpec()).ifPresent(requestConfigBuilder::setCookieSpec); builder.setDefaultRequestConfig(requestConfigBuilder.build()); builder.setDefaultSocketConfig(SocketConfig.copy(SocketConfig.DEFAULT) .setSoTimeout(config.getSocketTimeout()) .build()); builder.setDnsResolver(config.getDnsResolver()); HttpClient httpClient = new HttpClient(); httpClient.setCloseableHttpClient(builder.build()); if (config.hasBaseUrl()) { httpClient.setHttpHost(HttpHost.create(config.getBaseUrl())); } httpClient.setSkipResponseEntity(config.isSkipResponseEntity()); return httpClient; }
Example 10
Source File: HttpClientUtils.java From turbo-rpc with Apache License 2.0 | 4 votes |
public static CloseableHttpClient createHttpClient(int concurrency) { HttpClientBuilder builder = HttpClientBuilder.create(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setDefaultMaxPerRoute(concurrency); connManager.setMaxTotal(concurrency); RequestConfig requestConfig = RequestConfig.custom()// .setAuthenticationEnabled(true)// .setSocketTimeout(SOCKET_TIMEOUT)// .setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)// .setConnectTimeout(CONNECT_TIMEOUT)// .setRedirectsEnabled(true)// .setRelativeRedirectsAllowed(true)// .setMaxRedirects(15)// .build(); SocketConfig socketConfig = SocketConfig.custom()// .setSoKeepAlive(true)// .setSoReuseAddress(true)// .build(); CookieSpecProvider cookieSpecProvider = new IgnoreSpecProvider(); Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()// .register(CookieSpecs.DEFAULT, cookieSpecProvider)// .register(CookieSpecs.STANDARD, cookieSpecProvider)// .register(CookieSpecs.STANDARD_STRICT, cookieSpecProvider)// .build(); builder.setConnectionManager(connManager); builder.setDefaultSocketConfig(socketConfig); builder.setDefaultRequestConfig(requestConfig); builder.setDefaultCookieSpecRegistry(cookieSpecRegistry); return builder.addInterceptorLast((HttpRequest request, HttpContext context) -> { request.removeHeaders("Host"); request.removeHeaders("Accept-Encoding"); //request.removeHeaders("Connection"); request.removeHeaders("User-Agent"); }).build(); }
Example 11
Source File: HttpClient.java From ats-framework with Apache License 2.0 | 4 votes |
protected void initialzeInternalClient() { if (!needsInternalClientInialization) { // internal client is already initialized return; } // release any resources if this client was already used close(); // rebuild the client HttpClientBuilder httpClientBuilder = HttpClients.custom(); // Add this interceptor to get the values of all HTTP headers in the request. // Some of them are provided by the user while others are generated by Apache HTTP Components. httpClientBuilder.addInterceptorLast(new HttpRequestInterceptor() { @Override public void process( HttpRequest request, HttpContext context ) throws HttpException, IOException { Header[] requestHeaders = request.getAllHeaders(); actualRequestHeaders = new ArrayList<HttpHeader>(); for (Header header : requestHeaders) { addHeaderToList(actualRequestHeaders, header.getName(), header.getValue()); } if (debugLevel != HttpDebugLevel.NONE) { logHTTPRequest(requestHeaders, request); } } }); // connect and read timeouts httpClientBuilder.setDefaultRequestConfig(RequestConfig.custom() .setConnectTimeout(connectTimeoutSeconds * 1000) .setSocketTimeout(readTimeoutSeconds * 1000) .build()); // socket buffer size if (this.socketBufferSize > 0) { httpClientBuilder.setDefaultSocketConfig(SocketConfig.custom() .setRcvBufSize(this.socketBufferSize) .setSndBufSize(this.socketBufferSize) .build()); } // SSL if (isOverSsl) { setupSSL(httpClientBuilder); } // setup authentication if (!StringUtils.isNullOrEmpty(username)) { setupAuthentication(httpClientBuilder); } // set proxy if (AtsSystemProperties.SYSTEM_HTTP_PROXY_HOST != null && AtsSystemProperties.SYSTEM_HTTP_PROXY_PORT != null) { HttpHost proxy = new HttpHost(AtsSystemProperties.SYSTEM_HTTP_PROXY_HOST, Integer.parseInt(AtsSystemProperties.SYSTEM_HTTP_PROXY_PORT)); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); httpClientBuilder.setRoutePlanner(routePlanner); } // now build the client after we have already set everything needed on the client builder httpClient = httpClientBuilder.build(); // do not come here again until not needed needsInternalClientInialization = false; }
Example 12
Source File: HttpConnectionPoolBuilder.java From cyberduck with GNU General Public License v3.0 | 4 votes |
/** * @param proxy Proxy configuration * @param listener Log listener * @param prompt Prompt for proxy credentials * @return Builder for HTTP client */ public HttpClientBuilder build(final Proxy proxy, final TranscriptListener listener, final LoginCallback prompt) { final HttpClientBuilder configuration = HttpClients.custom(); // Use HTTP Connect proxy implementation provided here instead of // relying on internal proxy support in socket factory switch(proxy.getType()) { case HTTP: case HTTPS: final HttpHost h = new HttpHost(proxy.getHostname(), proxy.getPort(), Scheme.http.name()); if(log.isInfoEnabled()) { log.info(String.format("Setup proxy %s", h)); } configuration.setProxy(h); configuration.setProxyAuthenticationStrategy(new CallbackProxyAuthenticationStrategy(ProxyCredentialsStoreFactory.get(), host, prompt)); break; } configuration.setUserAgent(new PreferencesUseragentProvider().get()); final int timeout = preferences.getInteger("connection.timeout.seconds") * 1000; configuration.setDefaultSocketConfig(SocketConfig.custom() .setTcpNoDelay(true) .setSoTimeout(timeout) .build()); configuration.setDefaultRequestConfig(this.createRequestConfig(timeout)); configuration.setDefaultConnectionConfig(ConnectionConfig.custom() .setBufferSize(preferences.getInteger("http.socket.buffer")) .setCharset(Charset.forName(host.getEncoding())) .build()); if(preferences.getBoolean("http.connections.reuse")) { configuration.setConnectionReuseStrategy(new DefaultClientConnectionReuseStrategy()); } else { configuration.setConnectionReuseStrategy(new NoConnectionReuseStrategy()); } configuration.setRetryHandler(new ExtendedHttpRequestRetryHandler(preferences.getInteger("http.connections.retry"))); configuration.setServiceUnavailableRetryStrategy(new DisabledServiceUnavailableRetryStrategy()); if(!preferences.getBoolean("http.compression.enable")) { configuration.disableContentCompression(); } configuration.setRequestExecutor(new LoggingHttpRequestExecutor(listener)); // Always register HTTP for possible use with proxy. Contains a number of protocol properties such as the // default port and the socket factory to be used to create the java.net.Socket instances for the given protocol configuration.setConnectionManager(this.createConnectionManager(this.createRegistry())); configuration.setDefaultAuthSchemeRegistry(RegistryBuilder.<AuthSchemeProvider>create() .register(AuthSchemes.BASIC, new BasicSchemeFactory( Charset.forName(preferences.getProperty("http.credentials.charset")))) .register(AuthSchemes.DIGEST, new DigestSchemeFactory( Charset.forName(preferences.getProperty("http.credentials.charset")))) .register(AuthSchemes.NTLM, preferences.getBoolean("webdav.ntlm.windows.authentication.enable") && WinHttpClients.isWinAuthAvailable() ? new BackportWindowsNTLMSchemeFactory(null) : new NTLMSchemeFactory()) .register(AuthSchemes.SPNEGO, preferences.getBoolean("webdav.ntlm.windows.authentication.enable") && WinHttpClients.isWinAuthAvailable() ? new BackportWindowsNegotiateSchemeFactory(null) : new SPNegoSchemeFactory()) .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory()).build()); return configuration; }
Example 13
Source File: AbstractHttpTransportFactory.java From bender with Apache License 2.0 | 4 votes |
protected HttpClientBuilder getClientBuilder(boolean useSSL, String url, Map<String, String> stringHeaders, int socketTimeout) { HttpClientBuilder cb = HttpClientBuilder.create(); /* * Setup SSL */ if (useSSL) { /* * All trusting SSL context */ try { cb.setSSLContext(getSSLContext()); } catch (Exception e) { throw new RuntimeException(e); } /* * All trusting hostname verifier */ cb.setSSLHostnameVerifier(new HostnameVerifier() { public boolean verify(String s, SSLSession sslSession) { return true; } }); } /* * Add default headers */ ArrayList<BasicHeader> headers = new ArrayList<BasicHeader>(stringHeaders.size()); stringHeaders.forEach((k, v) -> headers.add(new BasicHeader(k, v))); cb.setDefaultHeaders(headers); /* * Set socket timeout and transport threads */ SocketConfig sc = SocketConfig.custom().setSoTimeout(socketTimeout).build(); cb.setDefaultSocketConfig(sc); cb.setMaxConnPerRoute(this.config.getThreads()); cb.setMaxConnTotal(this.config.getThreads()); return cb; }
Example 14
Source File: AbstractUnitTest.java From deprecated-security-ssl with Apache License 2.0 | 2 votes |
protected final CloseableHttpClient getHTTPClient() throws Exception { final HttpClientBuilder hcb = HttpClients.custom(); if (enableHTTPClientSSL) { log.debug("Configure HTTP client with SSL"); final KeyStore myTrustStore = KeyStore.getInstance("JKS"); myTrustStore.load(new FileInputStream(getAbsoluteFilePathFromClassPath("truststore.jks").toFile()), "changeit".toCharArray()); final KeyStore keyStore = KeyStore.getInstance(keystore.toLowerCase().endsWith("p12")?"PKCS12":"JKS"); keyStore.load(new FileInputStream(getAbsoluteFilePathFromClassPath(keystore).toFile()), "changeit".toCharArray()); final SSLContextBuilder sslContextbBuilder = SSLContexts.custom().useProtocol("TLS"); if (trustHTTPServerCertificate) { sslContextbBuilder.loadTrustMaterial(myTrustStore, null); } if (sendHTTPClientCertificate) { sslContextbBuilder.loadKeyMaterial(keyStore, "changeit".toCharArray()); } final SSLContext sslContext = sslContextbBuilder.build(); String[] protocols = null; if (enableHTTPClientSSLv3Only) { protocols = new String[] { "SSLv3" }; } else { protocols = new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" }; } final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, protocols, null, NoopHostnameVerifier.INSTANCE); hcb.setSSLSocketFactory(sslsf); } hcb.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(60 * 1000).build()); return hcb.build(); }