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();