0

I'm in trouble with Authenticator Android Method.

This method works and return 200 Code (HTTP_OK):

Authenticator.setDefault(new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("admin", "1234".toCharArray());
                }
            });

but to become dinamically i sent the user values:

urlConnection = new URL(params[0]);
user = params[1];
pass = params[2];

changed the authentication to:

return new PasswordAuthentication(user, pass.toCharArray())

made a test: user/pass: "test".

I want to receive a 401 Code (HTTP_UNAUTHORIZED)

when i debug and click f7/f8 sometimes i can see the 401 in response code, but i can't return it, because it seems that he enter in a loop inside:

httpUrlConnection.getResponseCode();

Here is a fragment of my code:

.....    

    httpUrlConnection.setConnectTimeout(2000);
    httpUrlConnection.connect();
    statusCode = httpUrlConnection.getResponseCode(); //debug stop here
    httpUrlConnection.disconnect(); 
} catch (IOException e) {
    e.printStackTrace();
}
return statusCode;

Any tips?

Acauã Pitta
  • 654
  • 1
  • 9
  • 16

1 Answers1

0

The problem was in the Server.

The authentication method it was "Basic".

Changed it to "Digest" the server tries only authenticate one time, not recursively until can log in.

Edit:

But pay attention, because

HttpUrlConnection does not support digest as pointed here

Acauã Pitta
  • 654
  • 1
  • 9
  • 16