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 Danniel Willian Nascimento <da...@gmail.com> on 2006/02/13 22:31:33 UTC

HttpMethodRetryHandler question

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.

Re: HttpMethodRetryHandler question

Posted by Danniel Willian Nascimento <da...@gmail.com>.
Yep, now I got it working. =)
Thanx a lot for the help folks!

Danniel Nascimento

2006/2/13, Oleg Kalnichevski <ol...@apache.org>:
>
> On Mon, 2006-02-13 at 19:22 -0300, Danniel Willian Nascimento wrote:
> > Hi Roland,
> >
> > It sure helped (I was just thinking how to get over that too =)  ), but
> I
> > think I did not express myself right.
> > The problem is when I try to cancel the authentication (like clicking
> the
> > CANCEL button or providing
> > wrong credentials).  Even if I provide the wrong credentials for more
> than 3
> > times,
> > the client still prompts for the uid/pwd.  Isn't that supposed to be
> handled
> > by
> > the retry handler?
>
> Danniel,
>
> This is the job of the credentials provider to ensure that the same
> credentials are not retried multiple times. The credentials provide is
> expected to return null if it does not want HttpClient to retry
> authentication.
>
> Hope this helps
>
> Oleg
>
>
> > Btw, you're right, the code is not in the authentication guide.  It's in
> the
> > Exception Handling
> > guide. My bad. =)
> >
> >
> > 2006/2/13, Roland Weber <ht...@dubioso.net>:
> > >
> > > Hi Danniel,
> > >
> > > the retry handler decides how to deal with communication problems.
> > > A missing authentication is not a communication problem. There is
> > > no retry handler code in the authentication guide:
> > > http://jakarta.apache.org/commons/httpclient/authentication.html
> > >
> > > If you are repeatedly prompted for a uid/pwd for the same domain,
> > > make sure you use the same HttpState object for all requests. If
> > > you don't pass an extra HttpState object (which you don't in the
> > > code snippet you sent), then you have to use the same HttpClient
> > > object. HttpClient uses HttpState to store the credentials, as
> > > mentioned here:
> > >
> > >
> http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/auth/CredentialsProvider.html
> > >
> > > hope that helps,
> > >   Roland
> > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail:
> httpclient-user-help@jakarta.apache.org
> > >
> > >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>

Re: HttpMethodRetryHandler question

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2006-02-13 at 19:22 -0300, Danniel Willian Nascimento wrote:
> Hi Roland,
> 
> It sure helped (I was just thinking how to get over that too =)  ), but I
> think I did not express myself right.
> The problem is when I try to cancel the authentication (like clicking the
> CANCEL button or providing
> wrong credentials).  Even if I provide the wrong credentials for more than 3
> times,
> the client still prompts for the uid/pwd.  Isn't that supposed to be handled
> by
> the retry handler?

Danniel,

This is the job of the credentials provider to ensure that the same
credentials are not retried multiple times. The credentials provide is
expected to return null if it does not want HttpClient to retry
authentication. 

Hope this helps

Oleg


> Btw, you're right, the code is not in the authentication guide.  It's in the
> Exception Handling
> guide. My bad. =)
> 
> 
> 2006/2/13, Roland Weber <ht...@dubioso.net>:
> >
> > Hi Danniel,
> >
> > the retry handler decides how to deal with communication problems.
> > A missing authentication is not a communication problem. There is
> > no retry handler code in the authentication guide:
> > http://jakarta.apache.org/commons/httpclient/authentication.html
> >
> > If you are repeatedly prompted for a uid/pwd for the same domain,
> > make sure you use the same HttpState object for all requests. If
> > you don't pass an extra HttpState object (which you don't in the
> > code snippet you sent), then you have to use the same HttpClient
> > object. HttpClient uses HttpState to store the credentials, as
> > mentioned here:
> >
> > http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/auth/CredentialsProvider.html
> >
> > hope that helps,
> >   Roland
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> >
> >


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: HttpMethodRetryHandler question

Posted by Danniel Willian Nascimento <da...@gmail.com>.
Hi Roland,

It sure helped (I was just thinking how to get over that too =)  ), but I
think I did not express myself right.
The problem is when I try to cancel the authentication (like clicking the
CANCEL button or providing
wrong credentials).  Even if I provide the wrong credentials for more than 3
times,
the client still prompts for the uid/pwd.  Isn't that supposed to be handled
by
the retry handler?
Btw, you're right, the code is not in the authentication guide.  It's in the
Exception Handling
guide. My bad. =)


2006/2/13, Roland Weber <ht...@dubioso.net>:
>
> Hi Danniel,
>
> the retry handler decides how to deal with communication problems.
> A missing authentication is not a communication problem. There is
> no retry handler code in the authentication guide:
> http://jakarta.apache.org/commons/httpclient/authentication.html
>
> If you are repeatedly prompted for a uid/pwd for the same domain,
> make sure you use the same HttpState object for all requests. If
> you don't pass an extra HttpState object (which you don't in the
> code snippet you sent), then you have to use the same HttpClient
> object. HttpClient uses HttpState to store the credentials, as
> mentioned here:
>
> http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/auth/CredentialsProvider.html
>
> hope that helps,
>   Roland
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>

Re: HttpMethodRetryHandler question

Posted by Roland Weber <ht...@dubioso.net>.
Hi Danniel,

the retry handler decides how to deal with communication problems.
A missing authentication is not a communication problem. There is
no retry handler code in the authentication guide:
http://jakarta.apache.org/commons/httpclient/authentication.html

If you are repeatedly prompted for a uid/pwd for the same domain,
make sure you use the same HttpState object for all requests. If
you don't pass an extra HttpState object (which you don't in the
code snippet you sent), then you have to use the same HttpClient
object. HttpClient uses HttpState to store the credentials, as
mentioned here:
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/auth/CredentialsProvider.html

hope that helps,
  Roland



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org