当前位置: 代码迷 >> J2SE >> 关于Authenticator.setDefault使用有关问题 求帮忙
  详细解决方案

关于Authenticator.setDefault使用有关问题 求帮忙

热度:57   发布时间:2016-04-23 20:13:22.0
关于Authenticator.setDefault使用问题 求帮忙
要实现一个用HttpsURLConnection获取基本验证的网站的html页面内容,然后使用了Authenticator.setDefault(Authenticator a)来设置请求的验证信息,问题来了,当我用第一个用户信息去获取数据时可以正常拿到数据,但当我再次调用Authenticator.setDefault(Authenticator a)更换用户信息想获取第二个用户的内容的时候,发现获取到的还是第一个用户的信息,经测试发现第二次设置没有生效请求还是用第一次的验证信息发出去的,
求解答为什么第二次调用Authenticator.setDefault(Authenticator a)为什么会没效果,怎么样处理才能让第二次请求以对应的用户信息发出去



部分代码如下:
         URL myURL = new URL("https://1.202.251.178/proj/rytong/pmmm/hours/query_hours?"+requestParams.toString());
        Authenticator.setDefault(new BasicAuthenticator(username,password));
        HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection(); 






public class BasicAuthenticator extends Authenticator {  
    String userName;  
    String password;  
  
    public BasicAuthenticator(String userName, String password) {  
        this.userName = userName;  
        this.password = password;  
    }  
  
    @Override  
    protected PasswordAuthentication getPasswordAuthentication() {  
        return new PasswordAuthentication(userName, password.toCharArray());  
    }  
}  

------解决思路----------------------
看一下老外找到的变通方法,他说这是个 bug,不知道是不是真的:
http://stackoverflow.com/questions/480895/reset-the-authenticator-credentials