NTCredentials ntCreds = new NTCredentials(ntUsername, ntPassword,localMachineName, domainName ); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(proxyHost,proxyPort), ntCreds ); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.useSystemProperties(); clientBuilder.setProxy(new HttpHost(pxInfo.getProxyURL(), pxInfo.getProxyPort())); clientBuilder.setDefaultCredentialsProvider(credsProvider); clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); CloseableHttpClient client = clientBuilder.build();
/** {@inheritDoc} */ @Override public DefaultHttpClientConfigurator configure(Config httpClientConfig) { Optional<HttpHost> proxy = getProxyAddr(httpClientConfig); if (proxy.isPresent()) { getBuilder().setProxy(proxy.get()); } return this; }
clientBuilder.setProxy(proxy);
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 = FhirContext.forDstu2(); 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); }
clientBuilder.setProxy(proxy); logger.debug("Working through Proxy: {}", proxy.getHostName());
public static void setProxy(final ProcessContext context, final HttpClientBuilder clientBuilder, final CredentialsProvider credentialsProvider) { // Set the proxy if specified final ProxyConfiguration proxyConfig = ProxyConfiguration.getConfiguration(context, () -> { if (context.getProperty(PROXY_HOST).isSet() && context.getProperty(PROXY_PORT).isSet()) { final ProxyConfiguration componentProxyConfig = new ProxyConfiguration(); final String host = context.getProperty(PROXY_HOST).getValue(); final int port = context.getProperty(PROXY_PORT).asInteger(); componentProxyConfig.setProxyType(Proxy.Type.HTTP); componentProxyConfig.setProxyServerHost(host); componentProxyConfig.setProxyServerPort(port); return componentProxyConfig; } return ProxyConfiguration.DIRECT_CONFIGURATION; }); if (Proxy.Type.HTTP.equals(proxyConfig.getProxyType())) { final String host = proxyConfig.getProxyServerHost(); final int port = proxyConfig.getProxyServerPort(); clientBuilder.setProxy(new HttpHost(host, port)); if (proxyConfig.hasCredential()) { final AuthScope proxyAuthScope = new AuthScope(host, port); final UsernamePasswordCredentials proxyCredential = new UsernamePasswordCredentials(proxyConfig.getProxyUserName(), proxyConfig.getProxyUserPassword()); credentialsProvider.setCredentials(proxyAuthScope, proxyCredential); } } }
hcBuilder.setDefaultCredentialsProvider(credsProvider); hcBuilder.setProxy(proxyHost);
private void setupProxyIfNeeded(HttpClientBuilder builder) { if (httpProxy != null) { builder.setProxy(new HttpHost(httpProxy.getHost(),httpProxy.getPort())); if (httpProxy.getUser() != null) { AuthScope proxyAuthScope = new AuthScope(httpProxy.getHost(),httpProxy.getPort()); UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(httpProxy.getUser(),httpProxy.getPass()); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(proxyAuthScope,proxyCredentials); builder.setDefaultCredentialsProvider(credentialsProvider); } } }
builder.setProxy(proxyHost);
.setConnectionManager(cm) .setDefaultRequestConfig(rcBuilder.build()) .setProxy(defaultProxy) .disableContentCompression().build();
Optional<HttpHost> proxyHost = createProxyHttpHost(proxy); if (proxyHost.isPresent()) { builder.setProxy(proxyHost.get()); Optional<BasicCredentialsProvider> credentialsProvider = createBasicCredentialsProvider( proxy, config.getProxyUser(), config.getProxyPass(),
private void resolveHttpParams(HttpParams params) { if (params != null) { clientBuilder.setDefaultRequestConfig(RequestConfig.custom() .setConnectTimeout(params.getConnectTimeout()) .setConnectionRequestTimeout(params.getReadTimeout()) .build()); if (params.getProxy() != null) { InetSocketAddress socketAddress = (InetSocketAddress) params .getProxy().address(); HttpHost proxy = new HttpHost(socketAddress.getHostName(), socketAddress.getPort()); clientBuilder.setProxy(proxy); } if (params.getSSLContext() != null) { clientBuilder .setSSLSocketFactory(new SSLConnectionSocketFactory( params.getSSLContext(), params.getHostnameVerifier() != null ? new CustomHostnameVerifier( params.getHostnameVerifier()) : SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)); } if (params.getHostnameVerifier() != null) { clientBuilder.setHostnameVerifier(new CustomHostnameVerifier( params.getHostnameVerifier())); } } }
@Test public void testNotMatchUrl() throws IOException, InterruptedException { URL flashbackScene = getClass().getResource(FLASHBACK_SCENE_DIR); String rootPath = flashbackScene.getPath(); SceneConfiguration sceneConfiguration = new SceneConfiguration(rootPath, SCENE_MODE, HTTP_SCENE); try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(SCENE_MODE) .sceneAccessLayer( new SceneAccessLayer(SceneFactory.create(sceneConfiguration), MatchRuleUtils.matchEntireRequest())) .build()) { flashbackRunner.start(); HttpHost host = new HttpHost(PROXY_HOST, PROXY_PORT); String url = "http://www.notexist.org/"; HttpClient client = HttpClientBuilder.create().setProxy(host).build(); HttpGet request = new HttpGet(url); HttpResponse httpResponse = client.execute(request); Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(), 400); Assert.assertTrue(EntityUtils.toString(httpResponse.getEntity()) .contains("No Matching Request")); url = "http://www.example.org/"; request = new HttpGet(url); request.addHeader("a", "b"); httpResponse = client.execute(request); Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(), 400); Assert.assertTrue(EntityUtils.toString(httpResponse.getEntity()) .contains("No Matching Request")); } }
@Test public void testNotMatchMethod() throws IOException, InterruptedException { URL flashbackScene = getClass().getResource(FLASHBACK_SCENE_DIR); String rootPath = flashbackScene.getPath(); SceneConfiguration sceneConfiguration = new SceneConfiguration(rootPath, SCENE_MODE, HTTP_SCENE); try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(SCENE_MODE) .sceneAccessLayer( new SceneAccessLayer(SceneFactory.create(sceneConfiguration), MatchRuleUtils.matchEntireRequest())) .build()) { flashbackRunner.start(); HttpHost host = new HttpHost(PROXY_HOST, PROXY_PORT); String url = "http://www.example.org/"; HttpClient client = HttpClientBuilder.create().setProxy(host).build(); HttpPost post = new HttpPost(url); HttpResponse httpResponse = client.execute(post); Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(), 400); Assert.assertTrue(EntityUtils.toString(httpResponse.getEntity()) .contains("No Matching Request")); } }
@Test() public void testReplayHttp() throws InterruptedException, IOException { URL flashbackScene = getClass().getResource(FLASHBACK_SCENE_DIR); String rootPath = flashbackScene.getPath(); SceneConfiguration sceneConfiguration = new SceneConfiguration(rootPath, SCENE_MODE, HTTP_SCENE); try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(SCENE_MODE) .sceneAccessLayer( new SceneAccessLayer(SceneFactory.create(sceneConfiguration), MatchRuleUtils.matchEntireRequest())) .build()) { flashbackRunner.start(); HttpHost host = new HttpHost(PROXY_HOST, PROXY_PORT); String url = "http://www.example.org/"; HttpClient client = HttpClientBuilder.create().setProxy(host).build(); HttpGet request = new HttpGet(url); HttpResponse httpResponse0 = client.execute(request); Assert.assertTrue(EntityUtils.toString(httpResponse0.getEntity()) .contains("I am from Flashback scene, not http://example.org")); url = "http://www.nba.com/"; request = new HttpGet(url); HttpResponse httpResponse1 = client.execute(request); Assert.assertTrue(EntityUtils.toString(httpResponse1.getEntity()) .contains("I am from Flashback scene, not http://www.nba.com")); url = "http://www.notexist.org/"; request = new HttpGet(url); HttpResponse httpResponse2 = client.execute(request); Assert.assertEquals(httpResponse2.getStatusLine().getStatusCode(), 400); Assert.assertTrue(EntityUtils.toString(httpResponse2.getEntity()) .contains("No Matching Request")); } }
@Test public void testNotMatchHeaders() throws IOException, InterruptedException { URL flashbackScene = getClass().getResource(FLASHBACK_SCENE_DIR); String rootPath = flashbackScene.getPath(); SceneConfiguration sceneConfiguration = new SceneConfiguration(rootPath, SCENE_MODE, HTTP_SCENE); try (FlashbackRunner flashbackRunner = new FlashbackRunner.Builder().mode(SCENE_MODE) .sceneAccessLayer( new SceneAccessLayer(SceneFactory.create(sceneConfiguration), MatchRuleUtils.matchEntireRequest())) .build()) { flashbackRunner.start(); HttpHost host = new HttpHost(PROXY_HOST, PROXY_PORT); String url = "http://www.example.org/"; HttpClient client = HttpClientBuilder.create().setProxy(host).build(); HttpGet request = new HttpGet(url); request.addHeader("a", "b"); HttpResponse httpResponse = client.execute(request); Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(), 400); Assert.assertTrue(EntityUtils.toString(httpResponse.getEntity()) .contains("No Matching Request")); } }
HttpHost host = new HttpHost(PROXY_HOST, PROXY_PORT); String url = "http://www.example.org/"; HttpClient client = HttpClientBuilder.create().setProxy(host).build(); HttpGet request = new HttpGet(url); request.addHeader("Set-Cookie",
/** * Assigns default proxy value. * <p> * Please note this value can be overridden by the {@link #setRoutePlanner( * org.apache.http.conn.routing.HttpRoutePlanner)} method. */ public final CustomHttpClientBuilder setProxy(final HttpHost proxy) { this.builder.setProxy(proxy); return this; }
private static CloseableHttpClient getClient(HttpHost proxy) { return HttpClients.custom() .setProxy(proxy) .build(); }
private static CloseableHttpClient getAllTrustClient(HttpHost proxy) { return HttpClients.custom() .setProxy(proxy) .setSslcontext(getSSLContext()) .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) .build(); }