@Override public HttpClient get() { HttpClientBuilder builder = HttpClientBuilder.create() .setMaxConnPerRoute(maxConnectionPerRoute) .setMaxConnTotal(maxTotalConnection); if (proxy.getProxyUrl() != null) { URL url = proxy.getProxyUrl(); builder.setProxy(new HttpHost(url.getHost(), url.getPort())); if (!Strings.isNullOrEmpty(proxy.getUsername()) && !Strings.isNullOrEmpty(proxy.getPassword())) { UsernamePasswordCredentials creds = new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()), creds); builder.setDefaultCredentialsProvider(credsProvider); builder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); } } return builder.build(); }
@Test public void testFcmClientWithProxySettings() throws Exception { // Create Settings: IFcmClientSettings settings = new FakeFcmClientSettings(); // Define the Credentials to be used: BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider(); // Set the Credentials (any auth scope used): basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("your_username", "your_password")); // Create the Apache HttpClientBuilder: HttpClientBuilder httpClientBuilder = HttpClientBuilder.create() // Set the Proxy Address: .setProxy(new HttpHost("your_hostname", 1234)) // Set the Authentication Strategy: .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()) // Set the Credentials Provider we built above: .setDefaultCredentialsProvider(basicCredentialsProvider); // Create the DefaultHttpClient: DefaultHttpClient httpClient = new DefaultHttpClient(settings, httpClientBuilder); // Finally build the FcmClient: try(IFcmClient client = new FcmClient(settings, httpClient)) { // TODO Work with the Proxy ... } }
public static void createHttpClient() { HttpClientBuilder clientBuilder = HttpClientBuilder.create(); String ip = System.getProperty("com.thesimego.proxy.ip"); String port = System.getProperty("com.thesimego.proxy.port"); String login = System.getProperty("com.thesimego.proxy.login"); String password = System.getProperty("com.thesimego.proxy.password"); if(ip != null && port != null && !ip.trim().isEmpty() && !port.trim().isEmpty()) { if(login != null && password != null && !login.trim().isEmpty() && !password.trim().isEmpty()) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(ip, Integer.parseInt(port)), new UsernamePasswordCredentials(login, password)); clientBuilder.setDefaultCredentialsProvider(credsProvider); } clientBuilder.useSystemProperties(); clientBuilder.setProxy(new HttpHost(ip, Integer.parseInt(port))); clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); } httpClient = clientBuilder.build(); BasicCookieStore cookieStore = new BasicCookieStore(); httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); }
public static void main(String[] args) { /* * This is out ot date - Just keeping * it in case it's helpful... */ final String authUser = "username"; final String authPassword = "password"; CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("10.10.10.10", 8080), new UsernamePasswordCredentials(authUser, authPassword)); HttpHost myProxy = new HttpHost("10.10.10.10", 8080); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder .setProxy(myProxy) .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()) .setDefaultCredentialsProvider(credsProvider) .disableCookieManagement(); CloseableHttpClient httpClient = clientBuilder.build(); FhirContext ctx = new FhirContext(); String serverBase = "http://spark.furore.com/fhir/"; ctx.getRestfulClientFactory().setHttpClient(httpClient); IGenericClient client = ctx.newRestfulGenericClient(serverBase); IdDt id = new IdDt("Patient", "123"); client.read(Patient.class, id); }
@Override public synchronized HttpClient getHttpClient() { if (myHttpClient == null) { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); //@formatter:off RequestConfig defaultRequestConfig = RequestConfig.custom() .setSocketTimeout(mySocketTimeout) .setConnectTimeout(myConnectTimeout) .setConnectionRequestTimeout(myConnectionRequestTimeout) .setStaleConnectionCheckEnabled(true) .setProxy(myProxy) .build(); HttpClientBuilder builder = HttpClients.custom() .setConnectionManager(connectionManager) .setDefaultRequestConfig(defaultRequestConfig) .disableCookieManagement(); if (myProxy != null && StringUtils.isNotBlank(myProxyUsername) && StringUtils.isNotBlank(myProxyPassword)) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(myProxy.getHostName(), myProxy.getPort()), new UsernamePasswordCredentials(myProxyUsername, myProxyPassword)); builder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); builder.setDefaultCredentialsProvider(credsProvider); } myHttpClient = builder.build(); //@formatter:on } return myHttpClient; }
public synchronized HttpClient getNativeHttpClient() { if (myHttpClient == null) { //FIXME potential resoource leak PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); connectionManager.setMaxTotal(getPoolMaxTotal()); connectionManager.setDefaultMaxPerRoute(getPoolMaxPerRoute()); // @formatter:off //TODO: Use of a deprecated method should be resolved. RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(getSocketTimeout()) .setConnectTimeout(getConnectTimeout()).setConnectionRequestTimeout(getConnectionRequestTimeout()) .setStaleConnectionCheckEnabled(true).setProxy(myProxy).build(); HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connectionManager) .setDefaultRequestConfig(defaultRequestConfig).disableCookieManagement(); if (myProxy != null && StringUtils.isNotBlank(getProxyUsername()) && StringUtils.isNotBlank(getProxyPassword())) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(myProxy.getHostName(), myProxy.getPort()), new UsernamePasswordCredentials(getProxyUsername(), getProxyPassword())); builder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); builder.setDefaultCredentialsProvider(credsProvider); } myHttpClient = builder.build(); // @formatter:on } return myHttpClient; }