protected CloseableHttpClient createDefaultHttpClient() { PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(maxConnections); cm.setDefaultMaxPerRoute(maxConnections); Builder config = RequestConfig.custom() .setConnectionRequestTimeout(connectionRequestTimeout) .setConnectTimeout(connectTimeout) .setSocketTimeout(socketTimeout); // TODO: Set Credentials CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(cm).setDefaultRequestConfig(config.build()) .build(); return httpClient; }
/** * 获取HttpClientBuilder对象,默认UserAgent为chrome * 默认ConnectTimeout DEFAULT_CONN_TIMEOUT 1000 * 默认SocketTimeout DEFAULT_SO_TIMEOUT 3000 * @param proxy Http代理 * @return */ public static HttpClientBuilder getClientBuilder(HttpHost proxy) { HttpClientBuilder builder = HttpClients.custom().setUserAgent(USER_AGENT); if (proxy != null) { builder.setProxy(proxy); } Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectionRequestTimeout(DEFAULT_CONN_REQ_TIMEOUT); requestConfigBuilder.setConnectTimeout(DEFAULT_CONN_TIMEOUT); requestConfigBuilder.setSocketTimeout(DEFAULT_SO_TIMEOUT); builder.setDefaultRequestConfig(requestConfigBuilder.build()); return builder; }
private RequestConfig getRequestConfig(boolean followRedirects) { Builder configBuilder = null; configBuilder = RequestConfig.custom(). setSocketTimeout(TIMEOUT). setConnectTimeout(TIMEOUT). setConnectionRequestTimeout(TIMEOUT). setRedirectsEnabled(followRedirects). setAuthenticationEnabled(false); // If a proxy was set to be used, set it if(getWebProxy() != null && !getWebProxy().trim().equals("") && getWebProxyPort() != null) { configBuilder.setProxy(new HttpHost(getWebProxy(), getWebProxyPort())); } return configBuilder.build(); }
private HttpClient createHttpClient(Authentication auth, String verify, HttpHost target, Boolean postRedirects, String password, TrustStrategy keystoreTrustStrategy, HostnameVerifier keystoreHostnameVerifier, Proxy proxy) { Certificate certificate = new Certificate(); Auth authHelper = new Auth(); HttpClientBuilder httpClientBuilder = WinHttpClients.custom(); Builder requestConfig = RequestConfig.custom(); requestConfig.setCookieSpec(CookieSpecs.DEFAULT); logger.debug("Verify value: " + verify); logger.debug((new File(verify).getAbsolutePath())); if (new File(verify).exists()) { logger.debug("Loading custom keystore"); httpClientBuilder.setSSLSocketFactory( certificate.allowAllCertificates(certificate.createCustomKeyStore(verify.toString(), password), password, keystoreTrustStrategy, keystoreHostnameVerifier)); } else if (!Boolean.parseBoolean(verify.toString())) { logger.debug("Allowing all certificates"); httpClientBuilder.setSSLSocketFactory(certificate.allowAllCertificates(null)); } if (auth.isAuthenticable()) { httpClientBuilder.setDefaultCredentialsProvider(authHelper.getCredentialsProvider(auth, target)); } if (proxy != null && proxy.isInUse()) { logger.debug("Enabling proxy"); if (proxy.isAuthenticable()) { logger.debug("Setting proxy credentials"); httpClientBuilder.setDefaultCredentialsProvider( authHelper.getCredentialsProvider(proxy.getAuth(), proxy.getHttpHost())); } requestConfig.setProxy(proxy.getHttpHost()); } if (postRedirects) { httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy()); } httpClientBuilder.setDefaultRequestConfig(requestConfig.build()); return httpClientBuilder.build(); }
public HttpRequestBase getRequest(){ String url = getUrl(); HttpRequestBase request = getRequest(url); if(!headers.isEmpty()){ for(Entry<String,List<String>> header : headers.entrySet()){ for(String headerValue : header.getValue()){ request.addHeader(header.getKey(), headerValue); } } } if(entity != null && canHaveEntity()){ ((HttpEntityEnclosingRequest) request).setEntity(entity); } if(timeoutMs != null || proxy != null){ Builder builder = RequestConfig.custom(); builder.setCookieSpec(CookieSpecs.STANDARD); if(timeoutMs != null){ builder.setConnectTimeout(timeoutMs).setConnectionRequestTimeout(timeoutMs).setSocketTimeout(timeoutMs); } if(proxy != null){ builder.setProxy(proxy); } RequestConfig requestConfig = builder.build(); request.setConfig(requestConfig); } return request; }
public static CloseableHttpClient getHttpClient(final HttpHost proxy, final boolean redirects, final CookieStore cookieStore) { final Builder builder = RequestConfig.custom() .setRedirectsEnabled(redirects) .setMaxRedirects(5); if (proxy != null) builder.setProxy(proxy); final RequestConfig requestConfig = builder.build(); return HttpClients.custom() .setDefaultRequestConfig(requestConfig) .setDefaultCookieStore(cookieStore) .setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE) .build(); }
private CloseableHttpResponse doRequest(final HttpRequestBase request) throws IOException { if (log.isDebugEnabled()) { log.debug(request.getMethod() + " " + request.getURI()); } Builder requestConfigBuilder = RequestConfig.custom().setSocketTimeout(check.getSocketTimeout()).setConnectTimeout(check.getConnectionTimeout()); if (check.getHttpProxyServer() != null && !check.getHttpProxyServer().isEmpty()) { HttpHost httpProxy = new HttpHost(check.getHttpProxyServer(), check.getHttpProxyPort()); requestConfigBuilder.setProxy(httpProxy); } RequestConfig requestConfig = requestConfigBuilder.build(); request.setConfig(requestConfig); String header = check.getHeader(); if(null!=header && header.length()>0 && header.contains(":")) { log.info("header:" + header); String[] headerKV = header.split(":"); request.setHeader(headerKV[0],headerKV[1]); } CloseableHttpResponse response = null; try { request.setHeader("User-Agent", check.getUserAgent()); response = httpClient.execute(request); } catch (SSLHandshakeException ex) { // ignore ValidatorException -> thrown when Java cannot validate // certificate log.error("java could not validate certificate for URL: " + request.getURI(), ex); return null; } if (log.isDebugEnabled()) { log.debug("status: " + response.getStatusLine()); } return response; }
/** * Initialize. * * @throws JAXBException * exception during create JAXB context. */ @BeforeClass public static final void init() throws JAXBException { final Builder builder = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(60000); final String proxy = System.getProperty("https_proxy"); if (StringUtils.isNotBlank(proxy)) { builder.setProxy(HttpHost.create(proxy)); } final RequestConfig config = builder.build(); client = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); context = JAXBContext.newInstance(Lohnsteuer.class); }
protected RequestConfig createDefaultRequestConfig(){ Builder configBuilder = RequestConfig.custom(); int socketTimeout = config.getInt("httpClient.request.socketTimeout"); int connTimeout = config.getInt("httpClient.request.connectTimeout"); int connReqTimeout = config.getInt("httpClient.request.connectionRequestTimeout"); log.info("Defeault request timeout: \n" + "\tsocketTimeout = {}\n" + "\tconnectionTimeout = {}\n" + "\tconnectionRequestTimeout = {}", socketTimeout, connTimeout, connReqTimeout); configBuilder.setSocketTimeout(socketTimeout); configBuilder.setConnectTimeout(connTimeout); configBuilder.setConnectionRequestTimeout(connReqTimeout); return configBuilder.build(); }
private static HttpClient createHttpClient(String clientCert, String clientKey, String serverCert) throws Exception { SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom() .loadTrustMaterial(KeyStoreUtil.createTrustStore(serverCert), null) .loadKeyMaterial(KeyStoreUtil.createKeyStore(clientCert, clientKey), "consul".toCharArray()) .build(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(setupSchemeRegistry(sslContext)); connectionManager.setMaxTotal(MAX_CONNECTION); connectionManager.setDefaultMaxPerRoute(MAX_ROUTE); Builder requestBuilder = RequestConfig.custom() .setConnectTimeout(DEFAULT_TIMEOUT_MS).setConnectionRequestTimeout(DEFAULT_TIMEOUT_MS).setSocketTimeout(DEFAULT_TIMEOUT_MS); return HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestBuilder.build()).build(); }
public void setChannelUrl(String channelUrl) { this.httpget = httpRequestFactory.createHttpGet(URI.create(channelUrl)); Builder requestConfigBuilder = RequestConfig.copy(defaultRequestConfig); httpget.setConfig(requestConfigBuilder.build()); httpget.setHeader(httpConstants.getHEADER_X_ATMOSPHERE_TRACKING_ID(), receiverId); if (channelUrl.length() > 15) { this.id = "..." + channelUrl.substring(channelUrl.length() - 15); } else { this.id = channelUrl; } }
/** * Get the access token * @return * @throws CloudException */ public Token getAccessToken() throws CloudException { logger.info("getAccessToken: '" + tokenUrl + "'"); try { SSLContextBuilder builder = new SSLContextBuilder(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder .<ConnectionSocketFactory> create().register("https", sslsf) .build(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); connectionManager.setMaxTotal(200); connectionManager.setDefaultMaxPerRoute(100); Builder requestConfigBuilder = RequestConfig.copy(RequestConfig.DEFAULT) .setSocketTimeout(getSocketTimeoutMS()) .setConnectTimeout(getConnectionTimeoutMS()) .setConnectionRequestTimeout(getConnectionRequestTimeoutMS()); if ((getProxyHost() != null) && (getProxyHost().length() > 0) && (getProxyPort() > 0)) { HttpHost proxy = new HttpHost(getProxyHost(), getProxyPort(), getProxyProtocol()); requestConfigBuilder.setProxy(proxy); } RequestConfig requestConfig = requestConfigBuilder.build(); HttpClient httpClient = HttpClients.custom() .setConnectionManager(connectionManager) .setDefaultRequestConfig(requestConfig) .setSSLSocketFactory(sslsf) .build(); List<NameValuePair> postParams = new ArrayList<NameValuePair>(); postParams.add(new BasicNameValuePair("grant_type", "apikey")); postParams.add(new BasicNameValuePair("key", key)); HttpPost postRequest = new HttpPost(tokenUrl); postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded"); postRequest.setEntity(new UrlEncodedFormEntity(postParams)); HttpResponse response = httpClient.execute(postRequest); if (response == null) throw new CloudException("Null response from http client: " + tokenUrl); if (response.getStatusLine() == null) throw new CloudException("Null status line from http client: " + tokenUrl); int statusCode = response.getStatusLine().getStatusCode(); logger.info("Status: " + statusCode); HttpEntity entity = response.getEntity(); if (entity == null) { throw new CloudException("Null response from Cloud Server"); } if (statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR) { throw new CloudException("Internal cloud server error: " + entity.toString()); } else if (statusCode != HttpStatus.SC_OK) { throw new CloudException( "HttpStatus: " + statusCode + " received from cloud server (" + entity.toString() + ")"); } byte[] returnedData = IOUtils.toByteArray(entity.getContent()); if (logger.isDebugEnabled()) { logger.debug("Reponse: " + new String(returnedData, "UTF-8")); } ObjectMapper mapper = new ObjectMapper(); Token token = mapper.readValue(returnedData, Token.class); return token; } catch (Exception e) { String message = String.format("%s thrown fetching token: %s", e.getClass().getSimpleName(), e.getMessage()); logger.error(message, e); throw new CloudException(message); } }
private HttpClient createHttpClient(String json) { Builder requestConfigBuilder = RequestConfig.custom(); Integer connPoolSize = null; try { Map settings = json != null ? new ObjectMapper().readValue(json, Map.class) : Collections.EMPTY_MAP; int timeout = timeoutFromSettings(settings, "connectTimeout"); logger.info("setting connectTimeout: " + timeout); requestConfigBuilder.setConnectTimeout(timeout); timeout = timeoutFromSettings(settings, "socketTimeout"); logger.info("setting socketTimeout: " + timeout); requestConfigBuilder.setSocketTimeout(timeout); timeout = timeoutFromSettings(settings, "connectionRequestTimeout"); logger.info("setting connectionRequestTimeout: " + timeout); requestConfigBuilder.setConnectionRequestTimeout(timeout); String proxyUrl = (String)settings.get("proxyUrl"); if (proxyUrl != null && !proxyUrl.isEmpty()) { logger.info("setting proxyUrl: " + proxyUrl); requestConfigBuilder.setProxy(HttpHost.create(proxyUrl)); } String connPoolSizeStr = (String)settings.get("connPoolSize"); if (connPoolSizeStr != null && !connPoolSizeStr.isEmpty()) { connPoolSize = Integer.valueOf(connPoolSizeStr); logger.info("setting ConnPoolSize: " + connPoolSize); } } catch (Exception e) { logger.error("could not read plugin settings", e); } HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setUserAgent(buildUserAgent()); clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build()); if (connPoolSize != null) { clientBuilder.setMaxConnPerRoute(connPoolSize.intValue()); } return clientBuilder.build(); }
protected RequestConfig getRequestConfig() { Builder builder = RequestConfig.custom().setCookieSpec(this.cookieSpec) .setAuthenticationEnabled(false) .setRedirectsEnabled(this.enableRedirects); return builder.build(); }
public HttpUriRequest prepareRequest(Uri uri, Job job) { Builder builder = RequestConfig.copy(defaultRequestConfig); HttpGet request = new HttpGet(uri.toURI()); // Add headers, cookies and ohter stuff if (headers() != null) { headers().forEach(header -> { if (header != null) { request.addHeader(header.getName(), header.getValue()); } }); } HttpParams params = new BasicHttpParams(); if (params() != null) { params().forEach(param -> { if (param != null) { params.setParameter(param.getName(), param.getValue()); } }); } request.setParams(params); // Configure the user -agent String userAgent = userAgentProvisionner().get(uri.toString(), job); if (Strings.isNullOrEmpty(userAgent)) { request.addHeader(HttpHeaders.USER_AGENT, userAgent); } // Configure the proxy ProxyServer proxy = proxyServersSource().findProxy(job); if (proxy != null) { // TODO Auth! HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getProtocol().getProtocol()); builder.setProxy(proxyHost); } request.setConfig(builder.build()); return request; }
public CloseableHttpClient getHttpClient() { Builder builder =RequestConfig.custom().setSocketTimeout(timeOutSecond * 1000) .setConnectTimeout(timeOutSecond * 1000); if(proxyIp!=null){ HttpHost proxyHost=new HttpHost(proxyIp.getIp(), proxyIp.getPort()); builder.setProxy(proxyHost); } RequestConfig defaultRequestConfig = builder.build(); // Create an HttpClient with the given custom dependencies and // configuration. HttpClientBuilder httpClientBuilder = HttpClients.custom().setUserAgent(userAgent)//.setMaxConnTotal(POOL_SIZE).setMaxConnPerRoute(POOL_SIZE) .setDefaultRequestConfig(defaultRequestConfig); //设置cookie if(cookies!=null&&cookies.size()>0) { BasicCookieStore cookieStore = new BasicCookieStore(); for(Cookie xmlCookie:cookies) { BasicClientCookie cookie = new BasicClientCookie(xmlCookie.getName(), xmlCookie.getValue()); if(xmlCookie.getHost()!=null) { cookie.setDomain(xmlCookie.getHost()); } if(xmlCookie.getPath()!=null) { cookie.setPath(xmlCookie.getPath()); } cookieStore.addCookie(cookie); } httpClientBuilder.setDefaultCookieStore(cookieStore); } CloseableHttpClient httpClient =httpClientBuilder.build(); return httpClient; }