下面这个程序,是用代理服务器(需要验证)访问网络,用正确的代理服务器地址用户名、账户 ,我自己测试是可以的
我查了一下Authenticator这个类是个虚拟类,但所有方法都没加abstract修饰。我把SimpleAuthenticator里唯一的方法去掉
,编译没问题,这好理解。但无法访问网络,这说明验证还是调用了getPasswordAuthentication()这个方法。
我的问题是为何这样设计呢,为何不在Authenticator类里把这个验证函数写实了呢?这样就不用用户去构造这个方法了,这样设计有何特别含义吗public class SimpleAuthenticator
   extends Authenticator
{
   private String username,
                  password;
    public SimpleAuthenticator(String username,String password)
   {
      this.username = username;
      this.password = password;
   }
    protected PasswordAuthentication getPasswordAuthentication()
   {
      return new PasswordAuthentication(
             username,password.toCharArray());
   }
}public class test {public static void main( String[] args ) {
  String url = "http://www.digitalcq.com/",
       proxy = "proxy.digitalcq.com",
       port = "8080",
       username = "usr",
       password = "pwd";
  Authenticator.setDefault(new SimpleAuthenticator(
       username,password));
  URL server = new URL(url);
  Properties systemProperties = System.getProperties();
  systemProperties.setProperty("http.proxyHost",proxy);
  systemProperties.setProperty("http.proxyPort",port);
  HttpURLConnection connection = (
    HttpURLConnection)server.openConnection();
  connection.connect();
  InputStream in = connection.getInputStream();
  readResponse(in);
}
}

解决方案 »

  1.   

    哥!你这代码有错误啊!  我现在在看片没有时间看。正确了在来!  ---------- 编译程序 ----------
    SimpleAuthenticator.java:18: 类 test 是公共的,应在名为 test.java 的文件中声明
    public class test {
           ^
    SimpleAuthenticator.java:2: 找不到符号
    符号: 类 Authenticator
      extends Authenticator
              ^
    SimpleAuthenticator.java:11: 找不到符号
    符号: 类 PasswordAuthentication
    位置: 类 SimpleAuthenticator
      protected PasswordAuthentication getPasswordAuthentication()
                ^
    SimpleAuthenticator.java:13: 找不到符号
    符号: 类 PasswordAuthentication
    位置: 类 SimpleAuthenticator
      return new PasswordAuthentication(
                 ^
    SimpleAuthenticator.java:26: 找不到符号
    符号: 变量 Authenticator
    位置: 类 test
      Authenticator.setDefault(new SimpleAuthenticator(
      ^
    SimpleAuthenticator.java:29: 找不到符号
    符号: 类 URL
    位置: 类 test
      URL server = new URL(url);
      ^
    SimpleAuthenticator.java:29: 找不到符号
    符号: 类 URL
    位置: 类 test
      URL server = new URL(url);
                       ^
    SimpleAuthenticator.java:30: 找不到符号
    符号: 类 Properties
    位置: 类 test
      Properties systemProperties = System.getProperties();
      ^
    SimpleAuthenticator.java:33: 找不到符号
    符号: 类 HttpURLConnection
    位置: 类 test
      HttpURLConnection connection = (
      ^
    SimpleAuthenticator.java:34: 找不到符号
    符号: 类 HttpURLConnection
    位置: 类 test
      HttpURLConnection)server.openConnection();
      ^
    SimpleAuthenticator.java:36: 找不到符号
    符号: 类 InputStream
    位置: 类 test
      InputStream in = connection.getInputStream();
      ^
    11 错误输出完成 (耗时 15 秒) - 正常终止
      

  2.   

    以后添加其他验证方式时直接写一个继承自Authenticator的类就可以,调用方式不变,比如,添加硬件验证Authenticator.setDefault(new HardwardAuthenticator(username,password));
      

  3.   

    这个应该是策略模式吧,因为具体的getPasswordAuthentication()方法对于不同的程序员来说实现方式不同,这样你要用自己的实现只需要实现自己的Authenticator类就行了,不用去改变原来的Authenticator类的代码,符合开闭原则。JDK里面很多这种类的,像Swing里面的组件模型一般都会有一个模型接口,一个实现接口的抽象类,还有一个Default版本的实现类。