How do I authenticate via HTTP in Android?
Asked
Active
Viewed 2,208 times
2
-
Yes I know, but i just intend to share my findings so that it could help other :) – Rohit Mandiwal Sep 02 '11 at 19:53
-
3Please at least clean up your code (`TODO Auto-generated method stub`, `System.err`, `rohit`?), format it nicely, **post it as an answer** to your own question and then accept it. – Philipp Reichart Sep 02 '11 at 20:09
-
This shouldn't be closed if Rohit will follow the advice from @Philipp. – John Saunders Sep 02 '11 at 21:20
-
Is this a dupe of this: [How to do HTTP authentication in android?](http://stackoverflow.com/questions/1968416/how-to-do-http-authentication-in-android) – abcd Sep 03 '11 at 16:46
-
Please, just edit your question if you wan to add additional information. – Tim Post Sep 03 '11 at 17:41
1 Answers
3
I face very much dificuly to authenticate via HTTP in Android as in browser (web and Android native) it worked perfect and ask for credentials but this scene is not in code. so here is the code I used.
URL url = new URL("YOUR URL HERE");
Authenticator.setDefault(new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
System.err.println("Feeding username and password for " + getRequestingScheme());
return (new PasswordAuthentication("username", "password".toCharArray()));
}});
InputStream stream = url.openStream();

Rohit Mandiwal
- 10,258
- 5
- 70
- 83
-
If this really is an answer to your question, you should accept it as such. – Tim Post Sep 10 '11 at 15:44