HTTPS链接时,android客户端认证的处理

来源:互联网 发布:321大数据蚌埠分公司 编辑:程序博客网 时间:2024/05/10 16:34

之前的android项目中没有接触过https的请求,在目前实习公司接手的项目中,用到了https的请求,为了私密起见,需要在发起请求之前进行授权认证,客户端可以通过这种写法进行认证

Authenticator.setDefault(new Authenticator() {            int retryCount = 0;//目前重试次数            int maxRetryCount = 1;//最大重试次数            protected PasswordAuthentication getPasswordAuthentication() {                // this condition is set to prevent Authenticator from sending                // multiple authentication requests on a bad login                if (retryCount < maxRetryCount) {                    retryCount++;                    return new PasswordAuthentication(userName, password                            .toCharArray());                }                return null;            }        });

也可以这样。。。当然,这并没有什么实质性改变,只是去除掉了多次验证的防范

class MyAuthenticator extends Authenticator{       String username = null;       String password = null;       public MyAuthenticator(username,password){           this.username = username;           this.password = password;       }     protected PasswordAuthentication getPasswordAuthentication(){        return new PasswordAuthentication(username,password);    }}//在需要进行验证的地方进行验证,放置这句代码Authenticator.setDefault(new MyAuthenticator(username,password));
0 0
原创粉丝点击