You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Arul Dhesiaseelan <ar...@acm.org> on 2013/12/23 07:55:52 UTC

Proxy authentication + Basic authentication on the Client instance

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

Re: Proxy authentication + Basic authentication on the Client instance

Posted by Arul Dhesiaseelan <ar...@acm.org>.
Awesome, that did the trick.

Thanks Oleg.


On Sun, Dec 22, 2013 at 10:42 PM, Oleg Kalnichevski <ol...@apache.org>wrote:

> 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
>
>

Re: Proxy authentication + Basic authentication on the Client instance

Posted by Oleg Kalnichevski <ol...@apache.org>.
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