List of usage examples for org.apache.http.impl.client HttpClientBuilder setDefaultHeaders
public final HttpClientBuilder setDefaultHeaders(final Collection<? extends Header> defaultHeaders)
From source file:com.gs.tools.doc.extractor.core.DownloadManager.java
private DownloadManager() { logger.info("Init DownloadManager"); cookieStore = new BasicCookieStore(); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setDefaultCookieStore(cookieStore); Collection<Header> headers = new ArrayList<Header>(); headers.add(new BasicHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")); headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36")); headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate,sdch")); headers.add(new BasicHeader("Accept-Language", "en-US,en;q=0.8")); // headers.add(new BasicHeader("Accept-Encoding", // "gzip,deflate,sdch")); clientBuilder.setDefaultHeaders(headers); ConnectionConfig.Builder connectionConfigBuilder = ConnectionConfig.custom(); connectionConfigBuilder.setBufferSize(10485760); clientBuilder.setDefaultConnectionConfig(connectionConfigBuilder.build()); SocketConfig.Builder socketConfigBuilder = SocketConfig.custom(); socketConfigBuilder.setSoKeepAlive(true); socketConfigBuilder.setSoTimeout(3000000); clientBuilder.setDefaultSocketConfig(socketConfigBuilder.build()); logger.info("Create HTTP Client"); httpClient = clientBuilder.build();/*from w ww .j a v a2 s . co m*/ }
From source file:com.basistech.rosette.api.HttpRosetteAPI.java
private void initClient(String key, List<Header> additionalHeaders) { HttpClientBuilder builder = HttpClients.custom(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(connectionConcurrency); builder.setConnectionManager(cm);//from w ww . j a v a 2 s . co m initHeaders(key, additionalHeaders); builder.setDefaultHeaders(this.additionalHeaders); httpClient = builder.build(); this.additionalHeaders = new ArrayList<>(); }
From source file:com.miapc.ipudong.Application.java
@Bean public RestTemplate getRestTemplate() { SSLContext sslcontext = null; Set<KeyManager> keymanagers = new LinkedHashSet<>(); Set<TrustManager> trustmanagers = new LinkedHashSet<>(); try {//from w w w .jav a 2 s .c o m trustmanagers.add(new HttpsTrustManager()); KeyManager[] km = keymanagers.toArray(new KeyManager[keymanagers.size()]); TrustManager[] tm = trustmanagers.toArray(new TrustManager[trustmanagers.size()]); sslcontext = SSLContexts.custom().build(); sslcontext.init(km, tm, new SecureRandom()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setSSLSocketFactory(factory); // ?3? httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(2, true)); // ????Keep-Alive httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()); List<Header> headers = new ArrayList<>(); headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36")); headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate")); headers.add(new BasicHeader("Accept-Language", "zh-CN")); headers.add(new BasicHeader("Connection", "Keep-Alive")); headers.add(new BasicHeader("Authorization", "reslibu")); httpClientBuilder.setDefaultHeaders(headers); CloseableHttpClient httpClient = httpClientBuilder.build(); if (httpClient != null) { // httpClient??RequestConfig HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); // clientHttpRequestFactory.setConnectTimeout(60 * 1000); // ???SocketTimeout clientHttpRequestFactory.setReadTimeout(5 * 60 * 1000); // ???? clientHttpRequestFactory.setConnectionRequestTimeout(5000); // ?truePOSTPUT????false? // clientHttpRequestFactory.setBufferRequestBody(false); // ? List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(); messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8"))); messageConverters.add(new MappingJackson2HttpMessageConverter()); messageConverters.add(new FormHttpMessageConverter()); messageConverters.add(new MappingJackson2XmlHttpMessageConverter()); RestTemplate restTemplate = new RestTemplate(messageConverters); restTemplate.setRequestFactory(clientHttpRequestFactory); restTemplate.setErrorHandler(new DefaultResponseErrorHandler()); return restTemplate; } else { return null; } }
From source file:com.nextdoor.bender.ipc.http.AbstractHttpTransportFactory.java
protected HttpClientBuilder getClientBuilder(boolean useSSL, String url, Map<String, String> stringHeaders, int socketTimeout) { HttpClientBuilder cb = HttpClientBuilder.create(); /*//w ww . ja v a2 s . c o m * 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; }
From source file:com.neiljbrown.brighttalk.channels.reportingapi.client.spring.AppConfig.java
/** * @return The instance of {@link HttpClient} to be used by {@link ClientHttpRequestFactory} to create client * requests. Pre-configured to support basic authentication using externally configured API user credentials, and to * utilise the API service's support for HTTP response compression (using gzip). *///from w w w .jav a 2s . c o m @Bean public HttpClient httpClient() { HttpClientBuilder builder = HttpClients.custom(); // Configure the basic authentication credentials to use for all requests CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); AuthScope authScope = new AuthScope(this.apiServiceHostName, this.apiServicePort); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(this.apiUserKey, this.apiUserSecret); credentialsProvider.setCredentials(authScope, credentials); builder.setDefaultCredentialsProvider(credentialsProvider); builder.addInterceptorFirst(new PreemptiveBasicAuthHttpRequestInterceptor()); // Configure default request headers List<Header> headers = new ArrayList<>(5); headers.add(new BasicHeader("Api-Client", SpringApiClientImpl.class.getCanonicalName())); if (this.defaultRequestHeaders != null) { for (String header : this.defaultRequestHeaders) { String[] headerNameAndValue = header.split("==", 2); if (headerNameAndValue.length == 2) { headers.add(new BasicHeader(headerNameAndValue[0], headerNameAndValue[1])); } } } builder.setDefaultHeaders(headers); // HttpClient should by default set the Accept-Encoding request header to indicate the client supports HTTP // response compression using gzip return builder.build(); }
From source file:edu.harvard.hms.dbmi.bd2k.irct.ri.i2b2.I2B2XMLResourceImplementation.java
/** * CREATES A CLIENT// w w w.j av a2 s . c o m * * @param token * @return */ protected HttpClient createClient(SecureSession session) { // SSL WRAPAROUND HttpClientBuilder returns = null; if (ignoreCertificate) { try { // CLIENT CONNECTION returns = ignoreCertificate(); } catch (NoSuchAlgorithmException | KeyManagementException e) { e.printStackTrace(); } } else { returns = HttpClientBuilder.create(); } List<Header> defaultHeaders = new ArrayList<Header>(); String token = session.getToken().toString(); if (this.clientId != null) { token = SecurityUtility.delegateToken(this.namespace, this.clientId, session); } if (session != null) { defaultHeaders.add(new BasicHeader("Authorization", token)); } defaultHeaders.add(new BasicHeader("Content-Type", "application/x-www-form-urlencoded")); returns.setDefaultHeaders(defaultHeaders); return returns.build(); }
From source file:com.floragunn.searchguard.httpclient.HttpClient.java
private final CloseableHttpClient createHTTPClient() throws NoSuchAlgorithmException, KeyStoreException, CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException, KeyManagementException { // basic auth // pki auth/*from w w w . j av a2 s . com*/ // kerberos auth final org.apache.http.impl.client.HttpClientBuilder hcb = HttpClients.custom(); if (ssl) { final SSLContextBuilder sslContextbBuilder = SSLContexts.custom().useTLS(); if (log.isTraceEnabled()) { log.trace("Configure HTTP client with SSL"); } if (trustStore != null) { final KeyStore myTrustStore = KeyStore .getInstance(trustStore.getName().endsWith("jks") ? "JKS" : "PKCS12"); myTrustStore.load(new FileInputStream(trustStore), truststorePassword == null || truststorePassword.isEmpty() ? null : truststorePassword.toCharArray()); sslContextbBuilder.loadTrustMaterial(myTrustStore); } if (keystore != null) { final KeyStore keyStore = KeyStore .getInstance(keystore.getName().endsWith("jks") ? "JKS" : "PKCS12"); keyStore.load(new FileInputStream(keystore), keystorePassword == null || keystorePassword.isEmpty() ? null : keystorePassword.toCharArray()); sslContextbBuilder.loadKeyMaterial(keyStore, keystorePassword == null || keystorePassword.isEmpty() ? null : keystorePassword.toCharArray()); } final SSLContext sslContext = sslContextbBuilder.build(); final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1.1", "TLSv1.2" }, null, verifyHostnames ? SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER : SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); hcb.setSSLSocketFactory(sslsf); } /*if (keytab != null) { //System.setProperty("java.security.auth.login.config", "login.conf"); //System.setProperty("java.security.krb5.conf", "krb5.conf"); final CredentialsProvider credsProvider = new BasicCredentialsProvider(); //SPNEGO/Kerberos setup log.debug("SPNEGO activated"); final AuthSchemeProvider nsf = new LoginSPNegoSchemeFactory(true); 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); }*/ if (basicCredentials != null) { hcb.setDefaultHeaders( Lists.newArrayList(new BasicHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicCredentials))); } return hcb.build(); }
From source file:crawlercommons.fetcher.http.SimpleHttpFetcher.java
private void init() { if (_httpClient == null) { synchronized (SimpleHttpFetcher.class) { if (_httpClient != null) return; final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); // Set the socket and connection timeout to be something // reasonable. requestConfigBuilder.setSocketTimeout(_socketTimeout); requestConfigBuilder.setConnectTimeout(_connectionTimeout); requestConfigBuilder.setConnectionRequestTimeout(_connectionRequestTimeout); /*// w w w. j a v a 2 s .c om * CoreConnectionPNames.TCP_NODELAY='http.tcp.nodelay': * determines whether Nagle's algorithm is to be used. Nagle's * algorithm tries to conserve bandwidth by minimizing the * number of segments that are sent. When applications wish to * decrease network latency and increase performance, they can * disable Nagle's algorithm (that is enable TCP_NODELAY. Data * will be sent earlier, at the cost of an increase in bandwidth * consumption. This parameter expects a value of type * java.lang.Boolean. If this parameter is not set, TCP_NODELAY * will be enabled (no delay). */ // FIXME Could not find this parameter in http-client version // 4.5 // HttpConnectionParams.setTcpNoDelay(params, true); // HttpProtocolParams.setVersion(params, _httpVersion); httpClientBuilder.setUserAgent(_userAgent.getUserAgentString()); // HttpProtocolParams.setContentCharset(params, "UTF-8"); // HttpProtocolParams.setHttpElementCharset(params, "UTF-8"); /* * CoreProtocolPNames.USE_EXPECT_CONTINUE= * 'http.protocol.expect-continue': activates the Expect: * 100-Continue handshake for the entity enclosing methods. The * purpose of the Expect: 100-Continue handshake is to allow the * client that is sending a request message with a request body * to determine if the origin server is willing to accept the * request (based on the request headers) before the client * sends the request body. The use of the Expect: 100-continue * handshake can result in a noticeable performance improvement * for entity enclosing requests (such as POST and PUT) that * require the target server's authentication. The Expect: * 100-continue handshake should be used with caution, as it may * cause problems with HTTP servers and proxies that do not * support HTTP/1.1 protocol. This parameter expects a value of * type java.lang.Boolean. If this parameter is not set, * HttpClient will not attempt to use the handshake. */ requestConfigBuilder.setExpectContinueEnabled(true); /* * CoreProtocolPNames.WAIT_FOR_CONTINUE= * 'http.protocol.wait-for-continue': defines the maximum period * of time in milliseconds the client should spend waiting for a * 100-continue response. This parameter expects a value of type * java.lang.Integer. If this parameter is not set HttpClient * will wait 3 seconds for a confirmation before resuming the * transmission of the request body. */ // FIXME Could not find this parameter in http-client version // 4.5 // params.setIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, // 5000); // FIXME Could not find this parameter in http-client version // 4.5 // CookieSpecParamBean cookieParams = new // CookieSpecParamBean(params); // cookieParams.setSingleHeader(false); // Create and initialize connection socket factory registry RegistryBuilder<ConnectionSocketFactory> registry = RegistryBuilder .<ConnectionSocketFactory>create(); registry.register("http", PlainConnectionSocketFactory.getSocketFactory()); SSLConnectionSocketFactory sf = createSSLConnectionSocketFactory(); if (sf != null) { registry.register("https", sf); } else { LOGGER.warn("No valid SSLContext found for https"); } _connectionManager = new PoolingHttpClientConnectionManager(registry.build()); _connectionManager.setMaxTotal(_maxThreads); _connectionManager.setDefaultMaxPerRoute(getMaxConnectionsPerHost()); /* * CoreConnectionPNames.STALE_CONNECTION_CHECK= * 'http.connection.stalecheck': determines whether stale * connection check is to be used. Disabling stale connection * check may result in a noticeable performance improvement (the * check can cause up to 30 millisecond overhead per request) at * the risk of getting an I/O error when executing a request * over a connection that has been closed at the server side. * This parameter expects a value of type java.lang.Boolean. For * performance critical operations the check should be disabled. * If this parameter is not set, the stale connection check will * be performed before each request execution. * * We don't need I/O exceptions in case if Server doesn't * support Kee-Alive option; our client by default always tries * keep-alive. */ // Even with stale checking enabled, a connection can "go stale" // between the check and the next request. So we still need to // handle the case of a closed socket (from the server side), // and disabling this check improves performance. // Stale connections will be checked in a separate monitor // thread _connectionManager.setValidateAfterInactivity(-1); httpClientBuilder.setConnectionManager(_connectionManager); httpClientBuilder.setRetryHandler(new MyRequestRetryHandler(_maxRetryCount)); httpClientBuilder.setRedirectStrategy(new MyRedirectStrategy(getRedirectMode())); httpClientBuilder.setRequestExecutor(new MyHttpRequestExecutor()); // FUTURE KKr - support authentication // FIXME Could not find this parameter in http-client version // 4.5 // HttpClientParams.setAuthenticating(params, false); requestConfigBuilder.setCookieSpec(CookieSpecs.DEFAULT); if (getMaxRedirects() == 0) { requestConfigBuilder.setRedirectsEnabled(false); } else { requestConfigBuilder.setRedirectsEnabled(true); requestConfigBuilder.setMaxRedirects(getMaxRedirects()); } // Set up default headers. This helps us get back from servers // what we want. HashSet<Header> defaultHeaders = new HashSet<Header>(); defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, getAcceptLanguage())); defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, DEFAULT_ACCEPT_CHARSET)); defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, DEFAULT_ACCEPT_ENCODING)); defaultHeaders.add(new BasicHeader(HttpHeaders.ACCEPT, DEFAULT_ACCEPT)); httpClientBuilder.setDefaultHeaders(defaultHeaders); httpClientBuilder.setKeepAliveStrategy(new MyConnectionKeepAliveStrategy()); monitor = new IdleConnectionMonitorThread(_connectionManager); monitor.start(); httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build()); _httpClient = httpClientBuilder.build(); } } }
From source file:org.flowable.ui.admin.service.engine.FlowableClientService.java
public CloseableHttpClient getHttpClient(String userName, String password) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password)); SSLConnectionSocketFactory sslsf = null; try {//from w w w. j av a 2 s . c o m SSLContextBuilder builder = new SSLContextBuilder(); builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); sslsf = new SSLConnectionSocketFactory(builder.build(), new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }); } catch (Exception e) { LOGGER.warn("Could not configure HTTP client to use SSL", e); } HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); if (preemptiveBasicAuthentication) { String auth = userName + ":" + password; httpClientBuilder.setDefaultHeaders(Collections.singletonList(new BasicHeader(AUTH.WWW_AUTH_RESP, "Basic " + Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8))))); } if (sslsf != null) { httpClientBuilder.setSSLSocketFactory(sslsf); } return httpClientBuilder.build(); }