hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Proxy authentication + Basic authentication on the Client instance
Date Mon, 23 Dec 2013 08:42:37 GMT
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: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message