2

How do I authenticate via HTTP in Android?

5
  • Yes I know, but i just intend to share my findings so that it could help other :) Commented Sep 2, 2011 at 19:53
  • 3
    Please 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. Commented Sep 2, 2011 at 20:09
  • This shouldn't be closed if Rohit will follow the advice from @Philipp. Commented Sep 2, 2011 at 21:20
  • Is this a dupe of this: How to do HTTP authentication in android? Commented Sep 3, 2011 at 16:46
  • Please, just edit your question if you wan to add additional information. Commented Sep 3, 2011 at 17:41

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

If this really is an answer to your question, you should accept it as such.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.