2

I am using HttpClient to connect to a host which requires BasicAUTH. But the proxy doesn't require any authentication. I have set it up as follows:

private final HttpClient httpClient; // Spring injected

Setting Basic auth:

private void setBasicAuth(final String username, final String password) {
    httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password));
    httpClient.getParams().setAuthenticationPreemptive(true);
}

Setting proxy:

private void setProxy(final String proxyHost, final int proxyPort) {
    hostConfiguration hostConfiguration = httpClient.getHostConfiguration();
    hostConfiguration.setProxy(proxyHost, proxyPort);
}

But I get the following warnings when running the code. Everything works, but I want to get rid of the warnings as well (or at least understand why they appears)

WARN  o.a.c.httpclient.HttpMethodDirector - Required proxy credentials not available for BASIC <any realm>@proxy.XXXXXX.no:3128
WARN  o.a.c.httpclient.HttpMethodDirector - Preemptive authentication requested but no default proxy credentials available

Any ideas?

Espen Herseth Halvorsen
  • 6,175
  • 7
  • 36
  • 38

1 Answers1

1

Here's an example from the Apache site for a proxy w/o credentials:

http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientExecuteProxy.java

(From http://hc.apache.org/httpcomponents-client-ga/examples.html)

You are getting the error because you are passing in a username/password and don't need to.

The WARN messages are coming from the logger (http://hc.apache.org/httpcomponents-client-ga/logging.html) - depending on how you have your logger set up you could just ignore that.

Having spent WAY too much time dealing with trying to make a Java application deal with proxy servers, I can tell you that using a tool such as Proxifier ( http://www.proxifier.com/ for Mac OS X and Windows) or CNTLM ( http://cntlm.sourceforge.net/) was much easier, more flexible, easier to debug, and kept the code clean.

Will Iverson
  • 2,009
  • 12
  • 22
  • Did you take into consideration that the host I am connecting to through the proxy does need me to set BasicAUTH for authentication? The example seems fine, but my problem is that the proxy doesn't need authentication, but the host I am proxying to does. It seems like I have misconfigured something since the proxy assumes I want it to use the BasicAUTH parameters. – Espen Herseth Halvorsen Apr 16 '11 at 01:34
  • The proxy should be handled by that example. The target Basic should be covered by this example http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientAuthentication.java You should be able to combine the two - or, if you use Proxifier (recommended), you only need this (second) one and your Java code doesn't need to worry about the proxy at all. – Will Iverson Apr 17 '11 at 00:14