Hi,
I've (very) recently started using httpclient (3.1) in my Java app. I've
successfully been able to make connections from a program running on server
A to another program (servlet) running on server B, where B uses Tomcat as
the servlet container, and DIGEST authentication is used to authenticate
between A and B.
Now, here is the problem: within server B, I need to make another http
request to a different servlet running also on B, in the same web
application as A. I used exactly the same code to create the Httpclient
instance, set credentials, and execute a GetMethod that does what I need on
that second servlet on B, that what I've used for connections between A and
B.
However, when this new httpclient connection is created and run within B, I
get a 'No credentials available for DIGEST: '<REALM NAME>&<DOMAIN NAME>'.
One thing to note (perhaps): on the web application on B, the http client
instances are created within a background thread that is part of the web
application.
Does anybody have any suggestions as to what could be causing this
credentials issue? (BTW, I'm printing out my own debugging statements when I
set the credentials in the httpclient instance, right before the call to
executeMethod).
My code is as follows:
private HttpClient getHttpClient() {
HttpClient client = new HttpClient();
List<String> authPrefs = new ArrayList<String>(1);
authPrefs.add(AuthPolicy.DIGEST);
client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY,
authPrefs);
client.getState().setCredentials(new AuthScope("mydomain", -1,
"myrealm"), new UsernamePasswordCredentials("myaccount", "mypassword")));
return client;
}
And I'm using it as follows :
HttpClient client = getHttpClient();
GetMethod get = new GetMethod("someurlWithinB");
get.setDoAuthentication(true);
int responseCode = 0;
String responseString = null;
try {
responseCode = client.executeMethod(get);
... (here I check the response code and find '401' )
} catch (Exception)
...
}
-Patricia