HttpsURLConnection Authenticator Error


HttpsURLConnection Authenticator Error



I'm in trouble with Authenticator Android Method.



This method works and return 200 Code (HTTP_OK):


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)


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?




1 Answer
1



The problem was in the Server.



The authentication method it was "Basic".


Basic



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


Digest



Edit:



But pay attention, because



HttpUrlConnection does not support digest as pointed here


HttpUrlConnection






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages