我以前没有遇到过这个特定的包,但它说它是用于客户端HTTP认证,我已经能够在Android上使用java.net API,如下所示:
Authenticator.setDefault(new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myuser","mypass".toCharArray());
}});
HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
c.setUseCaches(false);
c.connect();
显然,你的getPasswordAuthentication()应该做一些比返回一个常数更聪明。
如果你试图用身体(例如POST)身份验证请求,请小心Android issue 4326.我已经链接建议修复该平台,但有一个简单的解决方法,如果你只想要基本的验证:不要麻烦与Authenticator,而是这样做:
c.setRequestProperty("Authorization", "basic " +
Base64.encode("myuser:mypass".getBytes(), Base64.NO_WRAP));