Hi,
I have a web spider that retrieves the page's source codes using
HttpClient. As some sites require Authentication, I
have a class that implements CredentialProvider. I have also a class that
implements HttpMethodRetryHandler
(actually a copy from the Jakarta HttpClient Authentication Guide page).
The problem is that it never stops prompting for
the user and password. I think that somehow I'm not being able to register
the RetryHandler. Can someone help me
please, I'm really stuck here.
Here´s the snippet of the code for the HttpClient I have in my application:
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HttpClient client = new HttpClient();
HttpMethodRetryHandler myretryhandler = new HttpMethodRetryHandler() {
public boolean retryMethod(final HttpMethod method,
final IOException exception, int executionCount) {
System.out.println(executionCount); //THIS LINE HAS NO
EFFECT, THE COUNT IS NEVER PRINTED...
if (executionCount >= 3) {
return false;
}
if (exception instanceof NoHttpResponseException) {
return true;
}
if (!method.isRequestSent()) {
// Retry if the request has not been sent fully or
// if it's OK to retry methods that have been sent
return true;
}
if (exception instanceof
CredentialsNotAvailableException) {
//NOTE: tried
org.apache.commons.httpclient.ProtocolException also
// Authentication Failed
ExceptionDialog.showExceptionDialog("Authentication
Failed");
return false;
}
// otherwise do not retry
return false;
}
};
client.getParams().setParameter(CredentialsProvider.PROVIDER, new
myCredentialProvider() );
client.getParams().setParameter(HttpClientParams.RETRY_HANDLER,
myretryhandler);
//NOTE: I have tried "setParameter(HttpMethodParams.RETRY_HANDLER,
myretryhandler)" also with no success.
HttpMethod method = new GetMethod(url);
method.setDoAuthentication(true);
method.setRequestHeader("user-agent", "Mozilla/5.0");
int status = client.executeMethod(method);
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
And here´s the snippet of the code for the CredentialProvider:
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public Credentials getCredentials(final AuthScheme authscheme, final
String host,
int port, boolean proxy)
throws CredentialsNotAvailableException {
if (authscheme == null) {
return null;
}
try{
if (authscheme instanceof NTLMScheme) {
this.getPassword();
return new NTCredentials(this.user, this.password, host,
host);
} else if (authscheme instanceof RFC2617Scheme) {
this.getPassword();
return new UsernamePasswordCredentials(user, password);
} else {
throw new CredentialsNotAvailableException("Unsupported
authentication scheme: " +
authscheme.getSchemeName());
}
} catch (IOException e) {
e.printStackTrace();
throw new CredentialsNotAvailableException(e.getMessage(), e);
}
}
private void getPassword() {
//NOTE: the object "op" is a JOptionPane instantiated in the
constructor of the Panel
JDialog dialog = op.createDialog(this, "Authentication Required");
dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dialog.setVisible(true);
Object option = op.getValue();
if (option != null) {
if (opcao.equals(JOptionPane.OK_OPTION)) {
this.user = txtName.getText();
this.password = String.valueOf(txtPass.getPassword());
}
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Thanks a lot,
Danniel Nascimento.