2

How do I authenticate via HTTP in Android?

Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83

1 Answers1

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