On Sun, 2013-12-22 at 20:55 -1000, Arul Dhesiaseelan wrote:
> Hi,
>
> I am using a proxy to connect to a web service. Both proxy and web service
> are secured with different set of credentials. I am trying to perform a GET
> request using Apache HttpClient 4.3.1.
>
> URI proxyUri = URI.create("http://localhost:9000");
> URI targetUri = URI.create("http://localhost:8080");
> final HttpHost proxyHost = new HttpHost(proxyUri.getHost(),
> proxyUri.getPort(), proxyUri.getScheme());
> final HttpHost targetHost = new HttpHost(targetUri.getHost(),
> targetUri.getPort(), targetUri.getScheme());
> CredentialsProvider proxyCredentials = new BasicCredentialsProvider();
> proxyCredentials.setCredentials(
> new AuthScope(proxyUri.getHost(), proxyUri.getPort()),
> new UsernamePasswordCredentials("joe", "secret")
> );
>
> CredentialsProvider serviceCredentials = new BasicCredentialsProvider();
> serviceCredentials.setCredentials(
> new AuthScope(targetHost.getHostName(), targetHost.getPort()),
> new UsernamePasswordCredentials("user", "password"));
>
> CloseableHttpClient httpclient = HttpClients.custom()
> .setDefaultCredentialsProvider(serviceCredentials).build();// this
> sets only service creds
>
>
> It looks like you can set only one set of credentials at any time. I could
> not set both on the client. I tried something like this, but it would not
> allow me to set proxy credentials on the request config.
>
> RequestConfig config =
> RequestConfig.custom().setProxy(proxyHost).build();
> HttpGet httpGet = new HttpGet("/");
> httpGet.setConfig(config);
> System.out.println("executing request to " + targetHost + " via " +
> proxyHost);
>
> CloseableHttpResponse response = httpclient.execute(targetHost,
> httpGet);
>
> How can I use both of these credentials on the client? Is this supported?
>
> Thanks!
> Arul
Try this
---
URI proxyUri = URI.create("http://localhost:9000");
URI targetUri = URI.create("http://localhost:8080");
HttpHost proxyHost = new HttpHost(proxyUri.getHost(),
proxyUri.getPort(), proxyUri.getScheme());
HttpHost targetHost = new HttpHost(targetUri.getHost(),
targetUri.getPort(), targetUri.getScheme());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxyUri.getHost(), proxyUri.getPort()),
new UsernamePasswordCredentials("joe", "secret")
);
credsProvider.setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("user", "password"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
---
Oleg
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]