问题
How do I authenticate via HTTP in Android?
回答1:
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();
来源:https://stackoverflow.com/questions/7288526/android-http-authentication