下面这个程序,是用代理服务器(需要验证)访问网络,用正确的代理服务器地址用户名、账户 ,我自己测试是可以的
我查了一下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);
}
}
我查了一下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);
}
}
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 秒) - 正常终止